Python Practice › Set Programs › Program 22
Cartesian product of two sets
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
itertools.product; sort for a stable display.
Solution
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.