Python Program To Find Cartesian (Cross) Product Of Multiple List


This Python program calculates Cratesian (Cross) Product of multiple list using itertools.product. See Python source code with output below:

Python Source Code: Cross Product of Multiple List


import itertools

multiple_list = [
    [1, 2, 3],
    ['a', 'b', 'c'],
    ['x', 'y', 'z']
]

result = []
for item in itertools.product(*multiple_list):
    result.append(item)

print(result)

Output

[
	(1, 'a', 'x'), 
	(1, 'a', 'y'), 
	(1, 'a', 'z'), 
	(1, 'b', 'x'), 
	(1, 'b', 'y'), 
	(1, 'b', 'z'), 
	(1, 'c', 'x'), 
	(1, 'c', 'y'), 
	(1, 'c', 'z'), 
	(2, 'a', 'x'), 
	(2, 'a', 'y'), 
	(2, 'a', 'z'), 
	(2, 'b', 'x'), 
	(2, 'b', 'y'), 
	(2, 'b', 'z'), 
	(2, 'c', 'x'), 
	(2, 'c', 'y'), 
	(2, 'c', 'z'), 
	(3, 'a', 'x'), 
	(3, 'a', 'y'), 
	(3, 'a', 'z'), 
	(3, 'b', 'x'), 
	(3, 'b', 'y'), 
	(3, 'b', 'z'), 
	(3, 'c', 'x'), 
	(3, 'c', 'y'), 
	(3, 'c', 'z')
]