itertools.product()

This tool computes the cartesian product of input iterables. 

It is equivalent to nested for-loops. 

For example, product(A, B) returns the same as ((x,y) for x in A for y in B).  

Sample Code

>>> from itertools import product
>>>
>>> print list(product([1,2,3],repeat = 2))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
>>>
>>> print list(product([1,2,3],[3,4]))
[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
>>>
>>> A = [[1,2,3],[3,4,5]]
>>> print list(product(*A))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
>>>
>>> B = [[1,2,3],[3,4,5],[7,8]]
>>> print list(product(*B))
[(1, 3, 7), (1, 3, 8), (1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 3, 7), (2, 3, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (3, 3, 7), (3, 3, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8)]

Task

You are given a two lists $\mbox{A}$ and $\mbox{B}$. Your task is to compute their cartesian product $\mbox{A}$X$\mbox{B}$.

Example  

A = [1, 2]
B = [3, 4]

AxB = [(1, 3), (1, 4), (2, 3), (2, 4)]

Note: $\mbox{A}$ and $\mbox{B}$ are sorted lists, and the cartesian product's tuples should be output in sorted order.

Input Format

The first line contains the space separated elements of list $\mbox{A}$. 

The second line contains the space separated elements of list $\mbox{B}$.

Both lists have no duplicate integer elements.

Constraints

$0<A<30$ 

  $0<B<30$

Output Format

Output the space separated tuples of the cartesian product.

Sample Input
 1 2
 3 4

Sample Output
 (1, 3) (1, 4) (2, 3) (2, 4)

Difficulty: EASY

Solutions:
from itertools import product
A = map(int, input().split())
B = map(int, input().split())
C = list(product(A, B))
for tu in C:
	print('(%d, %d)' % tu, end=' ')


a = map(int, input().split())
b = map(int, input().split())
from itertools import product
print(' '.join(map(str, product(a, b))))


from itertools import product

def __starting_point():
	arr1 = list(map(int, input().strip().split(' ')))
	arr2 = list(map(int, input().strip().split(' ')))
	for el in product(arr1, arr2):
		print('{} '.format(el), end='')
__starting_point()


from itertools import product
L = [list(map(int, input().split()))]
L1 = [list(map(int, input().split()))]
L.extend(L1)
F = list(product(*L))
for i in F:
	print(i, end=' ')


from itertools import *
a = map(int, input().split())
b = map(int, input().split())
for i in product(a, b):
	print(i, end=' ')


from itertools import product
A = input('')
A1 = A.split()
A2 = []
for k in A1:
	A2.append(int(k))
B = input('')
B2 = []
B1 = B.split()
for k in B1:
	B2.append(int(k))
C = list(product(A2, B2))
for k in C:
	print(k, end=' ')


from itertools import product
print(' '.join(map(str, product(map(int, input().split()), map(int, input().split())))))


from itertools import product
A = tuple(map(int, input().split()))
B = tuple(map(int, input().split()))
ans = list(product(A, B))
print(' '.join([str(t) for t in ans]))


from itertools import product
import sys
state = 0
(A, B) = (0, 0)
for line in sys.stdin:
	if state == 0:
		A = tuple([int(e) for e in line.strip().split(' ')])
		state = 1
	elif state == 1:
		B = tuple([int(e) for e in line.strip().split(' ')])
		p = list(product(A, B))
		s = ''
		state = 0
		for e in p:
			s += str(e) + ' '
		print(s.strip())


from itertools import product
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(*list(product(a, b)), sep=' ')


import itertools
print(' '.join(map(str, list(itertools.product(map(int, input().split()), map(int, input().split()))))))


a = list(map(int, input().split()))
b = list(map(int, input().split()))
from itertools import product
for a in product(a, b):
	print(a, end=' ')


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(' '.join(map(str, list(product(A, B)))))


from itertools import product
A = input().split()
B = input().split()
print(' '.join(['({a}, {b})'.format(a=a, b=b) for (a, b) in product(A, B)]))


from itertools import product
a = map(int, input().split())
b = map(int, input().split())
print(' '.join(map(str, product(a, b))))


from itertools import product
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
print(*product(A, B))


import itertools
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
string = ''
for it in itertools.product(a, b):
	string += ' ' + str(it)
print(string[1:])


from itertools import product
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
tuples = list(product(a, b))
string = ''
for t in tuples:
	string += str(t) + ' '
print(string)


from itertools import product
line = input()
l = list(map(int, line.split()))
line = input()
l2 = list(map(int, line.split()))
for i in product(l, l2):
	print(i, end=' ')


from itertools import product
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
for i in list(product(a, b)):
	print(i, end=' ')


a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = [(x, y) for x in a for y in b]
for i in l:
	print(i, end=' ')


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(product(A, B))
res = ' '.join(map(str, C))
print(res)


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(' '.join([str(elem) for elem in product(A, B)]))


from itertools import product
A = map(int, input().split())
B = map(int, input().split())
C = product(A, B)
print(*C)


l1 = map(int, input().split())
l2 = map(int, input().split())
import itertools
for i in itertools.product(l1, l2):
	print(i, end=' ')


from itertools import product
(*a,) = map(int, input().split())
(*b,) = map(int, input().split())
print(' '.join(map(str, product(a, b))))


from itertools import product
s = list(map(int, input().split()))
l = list(map(int, input().split()))
m = list(product(s, l))
print(' '.join((repr(e) for e in m)))


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(*list(product(A, B)))


from itertools import product
str1 = input()
str2 = input()
A = list(map(int, str1.split()))
B = list(map(int, str2.split()))
C = list(product(A, B))
for i in range(len(C)):
	print(C[i], end='')
	if i != len(C) - 1:
		print(' ', end='')
print()


import itertools
a = list(map(int, input().split()))
b = list(map(int, input().split()))
a = itertools.product(a, b)
for i in a:
	print(i, end=' ')


from itertools import product
A = [int(x) for x in input().split()]
B = [int(x) for x in input().split()]
for t in product(A, B):
	print(t, end=' ')


A = list(map(int, input().split()))
B = list(map(int, input().split()))
from itertools import product
print(*list(product(A, B)))


from itertools import product
A = map(int, input().split())
B = map(int, input().split())
for i in product(A, B):
	print(i, end=' ')


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(product(A, B))
for i in C:
	print(i, end=' ')


from itertools import product
(A, B) = (list(map(int, input().split())), list(map(int, input().split())))
C = list(product(A, B))
print(' '.join((repr(e) for e in C)))


import itertools
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(' '.join(list(map(str, sorted(itertools.product(A, B))))))


from itertools import product
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
g = list(product(a, b))
for i in g:
	print(i, end=' ')


from itertools import product
A = list((int(i) for i in input().split()))
B = list((int(i) for i in input().split()))
C = sorted(product(A, B))
for i in C:
	print(i, end=' ')


from itertools import product
A = []
A.append(list(map(int, input().split())))
A.append(list(map(int, input().split())))
print(' '.join(list((str(i) for i in product(*A)))))


import itertools
A = map(int, input().strip().split())
B = map(int, input().strip().split())
R = list(itertools.product(A, B))
print(' '.join((str(i) for i in R)))


from itertools import *
print(' '.join((str(prod) for prod in list(product([int(x) for x in input().split()], [int(y) for y in input().split()])))))


import itertools
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(' '.join((str(e) for e in itertools.product(a, b))))


import itertools

def read_int():
	return int(input())

def read_int_words():
	return map(int, input().split())
a = read_int_words()
b = read_int_words()
out = list(itertools.product(a, b))
print(' '.join(map(str, out)))


from itertools import product
a = [int(i) for i in input().split()]
b = [int(j) for j in input().split()]
c = list(product(a, b))
for c in c:
	print(c, end=' ')


from itertools import product
a = map(int, input().strip().split())
b = map(int, input().strip().split())
print(' '.join(map(repr, product(a, b))))


listA = []
listB = []
a = input()
x = a.split()
for i in range(0, len(x)):
	listA.append(int(x[i]))
a = input()
x = a.split()
for i in range(0, len(x)):
	listB.append(int(x[i]))
for i in range(0, len(listA)):
	for j in range(0, len(listB)):
		list = [listA[i], listB[j]]
		print(tuple(list), end=' ')


from itertools import product
a = list(map(int, input().split()))
b = list(map(int, input().split()))
l = list(product(a, b))
print(' '.join([str(x) for x in l]))


x = [int(n) for n in input().split()]
y = [int(n) for n in input().split()]
for a in x:
	for b in y:
		c = (a, b)
		print(c, end=' ')


from itertools import product
s1 = map(int, input().split())
s2 = map(int, input().split())
l = list(product(s1, s2))
for i in l:
	print(i, end=' ')


from itertools import product
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(str(list(product(a, b))).replace('[', '').replace(']', '').replace('),', ')'))


from itertools import product
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(' '.join(map(str, list(product(a, b)))))


__author__ = 'namii'
from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
for item in product(A, B):
	print(item, end=' ')


from itertools import product
a = product(list(map(int, input().split())), list(map(int, input().split())))
for (m, n) in a:
	print((m, n), end=' ')


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
print(' '.join(map(str, product(A, B))))


from itertools import product
a = [int(k) for k in input().split()]
b = [int(k) for k in input().split()]
c = list(product(a, b))
for i in c:
	print(i, end=' ')


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = list(((x, y) for x in A for y in B))
for i in a:
	print(str(i) + ' ', end='')


from itertools import product
arr = list(map(int, input().strip().split()))
arr2 = list(map(int, input().strip().split()))
for i in list(product(arr, arr2)):
	print(i, end=' ')


from itertools import product
A = map(int, input().split())
B = map(int, input().split())
print(' '.join(('({}, {})'.format(a, b) for (a, b) in product(A, B))))


from itertools import product
a = list(product(map(int, input().split()), map(int, input().split())))
for i in range(len(a)):
	print(a[i], end=' ')


from itertools import *
a = input().split()
b = input().split()
for i in range(len(a)):
	a[i] = int(a[i])
for i in range(len(b)):
	b[i] = int(b[i])
l = list(product(a, b))
for i in l:
	print(i, end=' ')


from itertools import product
A = input().split()
A = [int(x) for x in A]
B = input().split()
B = [int(x) for x in B]
res = list(product(A, B))
for item in res:
	print(item, end=' ')


from itertools import product
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(' '.join([str(x) for x in list(product(a, b))]))


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
AxB = list(product(A, B))
output = ''
for i in AxB:
	output += str(i) + ' '
print(output)


from itertools import product
A = map(int, input().split())
B = map(int, input().split())
for item in product(A, B):
	print(item, end=' ')


import sys
from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
tuple1 = ()
tuple1 = tuple(product(A, B))
n = len(tuple1)
for i in range(0, n):
	print(tuple1[i], end=' ')


from itertools import product
a = set(map(int, input().split()))
b = set(map(int, input().split()))
[print(i, end=' ') for i in product(a, b)]


from itertools import product
a = list(map(int, input().split()))
b = list(map(int, input().split()))
print(' '.join((str(e) for e in product(a, b))))


import itertools
ar1 = list(map(int, input().split()))
ar2 = list(map(int, input().split()))
cross = list(itertools.product(ar1, ar2))
for i in cross:
	print(i, end=' ')


from itertools import product
l = list()
l.append(list(map(int, input().split())))
l.append(list(map(int, input().split())))
print(' '.join(map(str, product(*l))))


import itertools
A = map(int, input().split())
B = map(int, input().split())
print(' '.join(map(str, list(itertools.product(A, B)))))


from itertools import product
a = [int(c) for c in input().strip().split()]
b = [int(c) for c in input().strip().split()]
products = list(product(a, b))
formatted_products = ' '.join((str(t) for t in products))
print(formatted_products)


from itertools import product
a = map(int, input().split())
b = map(int, input().split())
for i in product(a, b):
	print(i, end=' ')


A = map(int, input().split())
B = map(int, input().split())
from itertools import product
print(' '.join([str(i) for i in list(product(A, B))]))


from itertools import product
A = list(map(int, input().split()))
B = list(map(int, input().split()))
a = list(product(A, B))
for i in range(len(a)):
	print(a[i], end=' ')


from itertools import product
print(' '.join(list(map(str, product(list(map(int, input().split())), list(map(int, input().split())))))))


from itertools import product
a = [int(i) for i in input().split()]
b = [int(i) for i in input().split()]
for i in product(a, b):
	print(i, end=' ')


import sys
from itertools import product
A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))
ans = list(product(A, B))
print(*ans)


from itertools import product
a = map(int, input().split())
b = map(int, input().split())
for item in product(a, b):
	print(item, end=' ')


from itertools import product
a = {int(i) for i in input().split()}
b = {int(i) for i in input().split()}
print(' '.join(map(str, product(a, b))))


from itertools import product
A = input()
A = list(map(int, A.split()))
B = input()
B = list(map(int, B.split()))
C = list(product(A, B))
for i in C:
	print(i, end=' ')


import sys, itertools
if sys.version_info[0] >= 3:
	raw_input = input
a = list(map(int, raw_input().split()))
b = list(map(int, raw_input().split()))
print(' '.join((str(e) for e in itertools.product(a, b))))


from itertools import product
(A, B) = list((input().strip().split() for _ in range(2)))
lis = list(product(A, B))
print(''.join(('(' + x[0] + ', ' + x[1] + ')' + ' ' for x in lis)))


A = [int(i) for i in input().split()]
B = [int(j) for j in input().split()]
for tup in [(i, j) for i in A for j in B]:
	print(tup, end=' ')


import itertools
a = map(int, input().split())
b = map(int, input().split())
print(' '.join(map(str, itertools.product(a, b))))


from itertools import product
from operator import itemgetter
A = map(int, input().split())
B = map(int, input().split())
AB = list(product(A, B))
sorted(AB, key=itemgetter(0))
print(' '.join([str(x) for x in AB]))


from itertools import product
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
print(' '.join([str(x) for x in product(a, b)]))


from itertools import product
print(' '.join([str(x) for x in list(product(map(int, input().split()), map(int, input().split())))]))


import itertools
A = str(input()).split()
A = [int(i) for i in A]
B = str(input()).split()
B = [int(i) for i in B]
L = list(itertools.product(A, B))
for T in L:
	print(T, end=' ')


import itertools
a = [int(x) for x in input().split()]
b = [int(x) for x in input().split()]
print(' '.join((str(t) for t in itertools.product(a, b))))