Is a set a superset?

Python Practice › Set Programs › Program 29

Is a set a superset?

EasySet Programs · 29 of 48

Write a Python program to check whether a set is a superset of another.

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

Approach

  1. >= or issuperset().

Solution

Python
print({1, 2, 3, 4} >= {2, 3}, {1, 2} >= {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

Symmetric difference in place

Python Practice › Set Programs › Program 28

Symmetric difference in place

EasySet Programs · 28 of 48

Write a Python program to update a set to the symmetric difference with another set.

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

Approach

  1. ^=.

Solution

Python
a = {1, 2, 3}a ^= {3, 4}print(a)
Output
{1, 2, 4}

Learn the concept

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

More set programs

← All 48 Set Programs

Union of several sets

Python Practice › Set Programs › Program 27

Union of several sets

EasySet Programs · 27 of 48

Write a Python program to find the union of several sets.

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

Approach

  1. set().union(*sets).

Solution

Python
sets = [{1, 2}, {2, 3}, {3, 4}]print(set().union(*sets))
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

Update a set to its intersection

Python Practice › Set Programs › Program 26

Update a set to its intersection

EasySet Programs · 26 of 48

Write a Python program to keep only the items shared with another set.

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

Approach

  1. intersection_update() or &=.

Solution

Python
a = {1, 2, 3, 4}a &= {2, 4, 6}print(a)
Output
{2, 4}

Learn the concept

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

More set programs

← All 48 Set Programs

difference() vs –

Python Practice › Set Programs › Program 25

difference() vs –

EasySet Programs · 25 of 48

Write a Python program to find the difference between two sets with the method and the operator.

Input
{"a", "b", "c"}, {"b"}
Output
['a', 'c'] ['a', 'c']

Approach

  1. difference() accepts any iterable; - needs sets.

Solution

Python
a = {"a", "b", "c"}print(sorted(a.difference(["b"])), sorted(a - {"b"}))
Output
['a', 'c'] ['a', 'c']

Learn the concept

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

More set programs

← All 48 Set Programs

Remove an arbitrary element

Python Practice › Set Programs › Program 24

Remove an arbitrary element

EasySet Programs · 24 of 48

Write a Python program to remove and return an arbitrary element with pop().

Input
{10, 20, 30}
Output
True 2

Approach

  1. pop() removes some element; you cannot choose which.

Solution

Python
s = {10, 20, 30}item = s.pop()print(item in {10, 20, 30}, len(s))
Output
True 2

Line by line

Line 2

Which element comes out is an implementation detail, so the program checks the result instead of printing a value that could differ.

Tip

For a truly random choice, use random.choice(list(s)).

Learn the concept

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

More set programs

← All 48 Set Programs

Clear a set

Python Practice › Set Programs › Program 23

Clear a set

EasySet Programs · 23 of 48

Write a Python program to remove all elements from a set.

Input
{1, 2, 3}
Output
set()

Approach

  1. clear().

Solution

Python
s = {1, 2, 3}s.clear()print(s)
Output
set()

Learn the concept

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

More set programs

← All 48 Set Programs

Is a set a subset of any other set?

Python Practice › Set Programs › Program 21

Is a set a subset of any other set?

EasySet Programs · 21 of 48

Write a Python program to check whether a set is a subset of any set in a list.

Input
{1, 2}; [{3, 4}, {1, 2, 5}, {6}]
Output
True

Approach

  1. any(target <= s for s in sets).

Solution

Python
target = {1, 2}sets = [{3, 4}, {1, 2, 5}, {6}]print(any(target <= s for s in sets))
Output
True

Learn the concept

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

More set programs

← All 48 Set Programs

Are all elements prime?

Python Practice › Set Programs › Program 20

Are all elements prime?

EasySet Programs · 20 of 48

Write a Python program to check whether all elements of a set are prime, and list the primes.

Input
{2, 3, 5, 9}
Output
False [2, 3, 5]

Approach

  1. A prime test for each element.

Solution

Python
def is_prime(n):    return n > 1 and all(n % d for d in range(2, int(n ** 0.5) + 1)) s = {2, 3, 5, 9}print(all(is_prime(x) for x in s), sorted(x for x in s if is_prime(x)))
Output
False [2, 3, 5]

Learn the concept

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

More set programs

← All 48 Set Programs

Are all elements odd?

Python Practice › Set Programs › Program 19

Are all elements odd?

EasySet Programs · 19 of 48

Write a Python program to check whether all elements of a set are odd.

Input
{1, 3, 5}, {1, 4}
Output
{1, 3, 5} True
{1, 4} False

Approach

  1. all(x % 2 == 1 for x in s).

Solution

Python
for s in [{1, 3, 5}, {1, 4}]:    print(s, all(x % 2 == 1 for x in s))
Output
{1, 3, 5} True
{1, 4} False

Learn the concept

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

More set programs

← All 48 Set Programs