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

Set to a list

Python Practice › Set Programs › Program 13

Set to a list

EasySet Programs · 13 of 48

Write a Python program to convert a set to a sorted list.

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

Approach

  1. sorted() returns a list in a predictable order.

Solution

Python
langs = {"python", "java", "go"}print(sorted(langs))
Output
['go', 'java', 'python']

Line by line

Line 2

list(langs) also works, but the order of string items can change between runs; sorted fixes it.

Learn the concept

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

More set programs

← All 48 Set Programs

List to a set

Python Practice › Set Programs › Program 12

List to a set

EasySet Programs · 12 of 48

Write a Python program to convert a list to a set.

Input
[4, 2, 4, 9, 2]
Output
{9, 2, 4}

Approach

  1. set(list) removes duplicates.

Solution

Python
print(set([4, 2, 4, 9, 2]))
Output
{9, 2, 4}

Learn the concept

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

More set programs

← All 48 Set Programs

Are two sets disjoint?

Python Practice › Set Programs › Program 11

Are two sets disjoint?

EasySet Programs · 11 of 48

Write a Python program to check whether two sets have no items in common.

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

Approach

  1. isdisjoint().

Solution

Python
print({1, 2}.isdisjoint({3, 4}), {1, 2}.isdisjoint({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 one set a subset of another?

Python Practice › Set Programs › Program 10

Is one set a subset of another?

EasySet Programs · 10 of 48

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

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

Approach

  1. <= or issubset(); < checks a proper subset.

Solution

Python
small, big = {2, 3}, {1, 2, 3, 4}print(small <= big, small < big, big <= small)
Output
True True False

Learn the concept

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

More set programs

← All 48 Set Programs

Symmetric difference

Python Practice › Set Programs › Program 9

Symmetric difference

EasySet Programs · 9 of 48

Write a Python program to find the items in exactly one of two sets.

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

Approach

  1. ^ or symmetric_difference().

Solution

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

Learn the concept

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

More set programs

← All 48 Set Programs

Difference of two sets

Python Practice › Set Programs › Program 8

Difference of two sets

EasySet Programs · 8 of 48

Write a Python program to find the items in the first set but not the second.

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

Approach

  1. - or difference(); the order of the sets matters.

Solution

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

Learn the concept

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

More set programs

← All 48 Set Programs