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

Remove an element from a set

Python Practice › Set Programs › Program 3

Remove an element from a set

EasySet Programs · 3 of 48

Write a Python program to remove an element from a set.

Input
{10, 20, 30}
Output
{10, 30}

Approach

  1. remove() raises KeyError if missing; discard() does not.

Solution

Python
s = {10, 20, 30}s.remove(20)s.discard(99)print(s)
Output
{10, 30}

Learn the concept

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

More set programs

← All 48 Set Programs

Add elements to a set

Python Practice › Set Programs › Program 2

Add elements to a set

EasySet Programs · 2 of 48

Write a Python program to add elements to a set.

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

Approach

  1. add() for one item; adding an existing item does nothing.

Solution

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

Learn the concept

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

More set programs

← All 48 Set Programs

Create a set

Python Practice › Set Programs › Program 1

Create a set

EasySet Programs · 1 of 48

Write a Python program to create a set and show that duplicates are removed.

Output
{8, 3, 5} 3
set 0

Approach

  1. Use curly braces with values, or set() for an empty set.

Solution

Python
numbers = {5, 3, 8, 3, 5}empty = set()print(numbers, len(numbers))print(type(empty).__name__, len(empty))
Output
{8, 3, 5} 3
set 0

Line by line

Line 1

The repeated 3 and 5 are stored once.

Line 2

{} would create a dictionary, so an empty set needs set().

Learn the concept

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

More set programs

← All 48 Set Programs