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

Are all elements even?

Python Practice › Set Programs › Program 18

Are all elements even?

EasySet Programs · 18 of 48

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

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

Approach

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

Solution

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

Learn the concept

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

More set programs

← All 48 Set Programs

Average of a set

Python Practice › Set Programs › Program 17

Average of a set

EasySet Programs · 17 of 48

Write a Python program to find the average of the elements of a set.

Input
{4, 8, 15, 16, 23, 42}
Output
18.0

Approach

  1. sum / len.

Solution

Python
s = {4, 8, 15, 16, 23, 42}print(sum(s) / len(s))
Output
18.0

Learn the concept

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

More set programs

← All 48 Set Programs

Sum of a set

Python Practice › Set Programs › Program 16

Sum of a set

EasySet Programs · 16 of 48

Write a Python program to add up the elements of a set.

Input
{10, 20, 30}
Output
60

Approach

  1. sum().

Solution

Python
print(sum({10, 20, 30}))
Output
60

Learn the concept

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

More set programs

← All 48 Set Programs

Minimum element of a set

Python Practice › Set Programs › Program 15

Minimum element of a set

EasySet Programs · 15 of 48

Write a Python program to find the smallest element in a set.

Input
{12, 45, 7, 33}
Output
7

Approach

  1. min().

Solution

Python
print(min({12, 45, 7, 33}))
Output
7

Learn the concept

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

More set programs

← All 48 Set Programs

Maximum element of a set

Python Practice › Set Programs › Program 14

Maximum element of a set

EasySet Programs · 14 of 48

Write a Python program to find the largest element in a set.

Input
{12, 45, 7, 33}
Output
45

Approach

  1. max().

Solution

Python
print(max({12, 45, 7, 33}))
Output
45

Learn the concept

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

More set programs

← All 48 Set Programs