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

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