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

Intersection of two sets

Python Practice › Set Programs › Program 7

Intersection of two sets

EasySet Programs · 7 of 48

Write a Python program to find the intersection of two sets.

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

Approach

  1. & or intersection().

Solution

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

Learn the concept

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

More set programs

← All 48 Set Programs

Union of two sets

Python Practice › Set Programs › Program 6

Union of two sets

EasySet Programs · 6 of 48

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

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

Approach

  1. | or union().

Solution

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

Learn the concept

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

More set programs

← All 48 Set Programs

Is an element in a set?

Python Practice › Set Programs › Program 5

Is an element in a set?

EasySet Programs · 5 of 48

Write a Python program to check whether an element is in a set.

Input
{2, 4, 6}, check 4 and 5
Output
True False

Approach

  1. in is very fast on sets.

Solution

Python
s = {2, 4, 6}print(4 in s, 5 in s)
Output
True False

Learn the concept

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

More set programs

← All 48 Set Programs

Length of a set

Python Practice › Set Programs › Program 4

Length of a set

EasySet Programs · 4 of 48

Write a Python program to find the number of items in a set.

Input
{"a", "b", "c", "a"}
Output
3

Approach

  1. len() counts unique items.

Solution

Python
print(len({"a", "b", "c", "a"}))
Output
3

Learn the concept

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

More set programs

← All 48 Set Programs