Cartesian product of two sets

Python Practice › Set Programs › Program 22

Cartesian product of two sets

EasySet Programs · 22 of 48

Write a Python program to find the cartesian product of two sets.

Input
{1, 2}, {"a", "b"}
Output
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

Approach

  1. itertools.product; sort for a stable display.

Solution

Python
from itertools import productprint(sorted(product({1, 2}, {"a", "b"})))
Output
[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]

Learn the concept

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

More set programs

← All 48 Set Programs

Longest word in a set

Python Practice › Set Programs › Program 48

Longest word in a set

EasySet Programs · 48 of 48

Write a Python program to find the longest word in a set.

Input
{"cat", "elephant", "dog", "hippopotamus"}
Output
hippopotamus

Approach

  1. max(s, key=len).

Solution

Python
print(max({"cat", "elephant", "dog", "hippopotamus"}, key=len))
Output
hippopotamus
Note

If two words share the longest length, the result depends on set order; use max(sorted(s), key=len) to make it predictable.

Learn the concept

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

More set programs

← All 48 Set Programs

Books two readers both own

Python Practice › Set Programs › Program 47

Books two readers both own

EasySet Programs · 47 of 48

Write a Python program to create two sets of books and find the books in both.

Output
['Atomic Habits', 'Wings of Fire']

Approach

  1. &.

Solution

Python
asha = {"Wings of Fire", "The Alchemist", "Atomic Habits"}ravi = {"Atomic Habits", "Sapiens", "Wings of Fire"}print(sorted(asha & ravi))
Output
['Atomic Habits', 'Wings of Fire']

Learn the concept

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

More set programs

← All 48 Set Programs

Set of favourite movies

Python Practice › Set Programs › Program 46

Set of favourite movies

EasySet Programs · 46 of 48

Write a Python program to create a set of favourite movies and add one more.

Output
['Dangal', 'Inception', 'Interstellar', 'Lagaan']

Approach

  1. add() then print sorted.

Solution

Python
movies = {"Inception", "Dangal", "Interstellar"}movies.add("Lagaan")print(sorted(movies))
Output
['Dangal', 'Inception', 'Interstellar', 'Lagaan']

Learn the concept

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

More set programs

← All 48 Set Programs

Set of favourite actors

Python Practice › Set Programs › Program 45

Set of favourite actors

EasySet Programs · 45 of 48

Write a Python program to create a set of your favourite actors.

Output
4 ['Akshay Kumar', 'Robert Pattinson', 'Shah Rukh Khan', 'Tom Hardy']

Approach

  1. A set of strings; print it sorted.

Solution

Python
actors = {"Shah Rukh Khan", "Akshay Kumar", "Tom Hardy", "Robert Pattinson", "Akshay Kumar"}print(len(actors), sorted(actors))
Output
4 ['Akshay Kumar', 'Robert Pattinson', 'Shah Rukh Khan', 'Tom Hardy']

Line by line

Line 1

“Akshay Kumar” appears twice but is stored once.

Learn the concept

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

More set programs

← All 48 Set Programs

Set of odd numbers

Python Practice › Set Programs › Program 44

Set of odd numbers

EasySet Programs · 44 of 48

Write a Python program to create a set of odd numbers from 1 to 20.

Output
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

Approach

  1. set(range(1, 21, 2)).

Solution

Python
print(set(range(1, 21, 2)))
Output
{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}

Learn the concept

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

More set programs

← All 48 Set Programs

Set of even numbers

Python Practice › Set Programs › Program 43

Set of even numbers

EasySet Programs · 43 of 48

Write a Python program to create a set of even numbers from 1 to 20.

Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Approach

  1. A set comprehension.

Solution

Python
print({n for n in range(1, 21) if n % 2 == 0})
Output
{2, 4, 6, 8, 10, 12, 14, 16, 18, 20}

Learn the concept

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

More set programs

← All 48 Set Programs

Set to a dictionary

Python Practice › Set Programs › Program 42

Set to a dictionary

EasySet Programs · 42 of 48

Write a Python program to convert a set into a dictionary.

Input
{"a", "b", "c"}
Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}

Approach

  1. dict.fromkeys(s, value) or a comprehension.

Solution

Python
s = {"a", "b", "c"}print(dict.fromkeys(sorted(s), 0))print({x: ord(x) for x in sorted(s)})
Output
{'a': 0, 'b': 0, 'c': 0}
{'a': 97, 'b': 98, 'c': 99}

Learn the concept

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

More set programs

← All 48 Set Programs

“Index” of an element in a set

Python Practice › Set Programs › Program 41

"Index" of an element in a set

EasySet Programs · 41 of 48

Write a Python program to find the position of an element in a set.

Input
{30, 10, 20}, element 20
Output
[10, 20, 30] 1

Approach

  1. Sets have no index. Sort into a list first, then use index().

Solution

Python
s = {30, 10, 20}ordered = sorted(s)print(ordered, ordered.index(20))
Output
[10, 20, 30] 1
Note

A set is unordered, so a position only means something after you convert it to a sorted list.

Learn the concept

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

More set programs

← All 48 Set Programs

Set elements found in a string

Python Practice › Set Programs › Program 40

Set elements found in a string

EasySet Programs · 40 of 48

Write a Python program to find which elements of a set appear in a string.

Input
{"py", "java", "test"}, "python testing"
Output
['py', 'test']

Approach

  1. [w for w in s if w in text].

Solution

Python
words = {"py", "java", "test"}text = "python testing"print(sorted(w for w in words if w in text))
Output
['py', 'test']

Learn the concept

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

More set programs

← All 48 Set Programs