Intersection of several sets

Python Practice › Set Programs › Program 39

Intersection of several sets

EasySet Programs · 39 of 48

Write a Python program to find the items shared by several sets.

Input
{1, 2, 3}, {2, 3, 4}, {3, 4, 5}
Output
{3}

Approach

  1. set.intersection(*sets).

Solution

Python
sets = [{1, 2, 3}, {2, 3, 4}, {3, 4, 5}]print(set.intersection(*sets))
Output
{3}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Difference with several sets

Python Practice › Set Programs › Program 38

Difference with several sets

EasySet Programs · 38 of 48

Write a Python program to remove from one set the items of several others.

Input
{1, 2, 3, 4, 5}, {2}, {4, 5}
Output
{1, 3}

Approach

  1. difference() accepts several sets.

Solution

Python
print({1, 2, 3, 4, 5}.difference({2}, {4, 5}))
Output
{1, 3}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Create a frozenset

Python Practice › Set Programs › Program 37

Create a frozenset

EasySet Programs · 37 of 48

Write a Python program to create a frozenset and show that it cannot be changed.

Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'

Approach

  1. frozenset(iterable); it has no add method.

Solution

Python
fs = frozenset([3, 1, 2])print(fs)try:    fs.add(4)except AttributeError as e:    print("Error:", e)
Output
frozenset({1, 2, 3})
Error: 'frozenset' object has no attribute 'add'

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Is a value a frozenset?

Python Practice › Set Programs › Program 36

Is a value a frozenset?

EasySet Programs · 36 of 48

Write a Python program to check whether a value is a frozenset.

Input
frozenset({1, 2}), {1, 2}
Output
frozenset True
set False

Approach

  1. isinstance(x, frozenset).

Solution

Python
for x in [frozenset({1, 2}), {1, 2}]:    print(type(x).__name__, isinstance(x, frozenset))
Output
frozenset True
set False

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Are two sets equal?

Python Practice › Set Programs › Program 35

Are two sets equal?

EasySet Programs · 35 of 48

Write a Python program to check whether two sets are equal.

Input
{1, 2, 3}, {3, 2, 1}
Output
True False

Approach

  1. == compares contents; order does not matter.

Solution

Python
print({1, 2, 3} == {3, 2, 1}, {1, 2} == {1, 2, 3})
Output
True False

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Is a set empty?

Python Practice › Set Programs › Program 34

Is a set empty?

EasySet Programs · 34 of 48

Write a Python program to check whether a set is empty.

Input
set(), {1}
Output
set() empty
{1} not empty

Approach

  1. Empty sets are falsy.

Solution

Python
for s in [set(), {1}]:    print(s, "empty" if not s else "not empty")
Output
set() empty
{1} not empty

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Remove several elements

Python Practice › Set Programs › Program 33

Remove several elements

EasySet Programs · 33 of 48

Write a Python program to remove several elements at once.

Input
{1, 2, 3, 4, 5}, remove [2, 4]
Output
{1, 3, 5}

Approach

  1. difference_update() or -=.

Solution

Python
s = {1, 2, 3, 4, 5}s -= {2, 4}print(s)
Output
{1, 3, 5}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Add several elements

Python Practice › Set Programs › Program 32

Add several elements

EasySet Programs · 32 of 48

Write a Python program to add several elements at once.

Input
{1, 2}, add [3, 4, 2]
Output
{1, 2, 3, 4}

Approach

  1. update() takes any iterable.

Solution

Python
s = {1, 2}s.update([3, 4, 2])print(s)
Output
{1, 2, 3, 4}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Remove a specific element safely

Python Practice › Set Programs › Program 31

Remove a specific element safely

EasySet Programs · 31 of 48

Write a Python program to remove a specific element, reporting whether it was there.

Input
{5, 10, 15}, remove 10 and 99
Output
removed 10
99 not in set
{5, 15}

Approach

  1. Check with in, then remove().

Solution

Python
s = {5, 10, 15}for x in [10, 99]:    if x in s:        s.remove(x)        print("removed", x)    else:        print(x, "not in set")print(s)
Output
removed 10
99 not in set
{5, 15}

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs

Common elements

Python Practice › Set Programs › Program 30

Common elements

EasySet Programs · 30 of 48

Write a Python program to find the elements common to two sets of words.

Input
{"python", "java", "sql"}, {"sql", "python", "go"}
Output
['python', 'sql']

Approach

  1. &, then sorted for display.

Solution

Python
a = {"python", "java", "sql"}b = {"sql", "python", "go"}print(sorted(a & b))
Output
['python', 'sql']

Learn the concept

New to this topic? Read the lesson, then come back.

More set programs

← All 48 Set Programs