Cartesian product of two sets
Write a Python program to find the cartesian product of two sets.
{1, 2}, {"a", "b"}[(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"})))[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')]
Learn the concept
New to this topic? Read the lesson, then come back.