The itertools module provides useful tools for working with iterables through iterators.

The product() function in the module generates  the cartesian product of arbitrary input iterables. Cartesian product of two sets A and B is the set of all possible ordered pairs (a, b) where a∈A and b∈B

Syntax:
product(*iterables, repeat = 1)
copy
*iterables Arbitrary iterables to get the cartesian products of. 
repeat Optional.  Specifies the number of times each element should be repeated. The default value is 1.
Parameters:

The product() function returns an iterator object which yields a tuple in each iteration. Each tuple represents a single combination in the cartesian product of the input iterables.

ExampleEdit & Run
from itertools import product

data1 = [1, 2]
data2 = [10, 20]

result = product(data1, data2)
print(result)

print(*result)
copy
Output:
<itertools.product object at 0x7f3ea857a480> (1, 10) (1, 20) (2, 10) (2, 20) [Finished in 0.010593411047011614s]

Note that the return value is an iterator object, we can therefore use the next() function to access a single value from the iterator, as shown below:

ExampleEdit & Run
from itertools import product

data1 = [1, 2]
data2 = [10, 20]

result = product(data1, data2)

print(next(result))
print(next(result))
print(next(result))
print(next(result))
print(next(result))
copy
Output:
(1, 10) (1, 20) (2, 10) (2, 20) Traceback (most recent call last):   File "<string>", line 12, in <module> StopIteration [Finished in 0.010159257799386978s]

We can also cast the returned iterator object into a list or other iterable types.

ExampleEdit & Run
from itertools import product

data1 = [1, 2]
data2 = [10, 20]

result = product(data1, data2)

L = list(result) #turn the iterator into a list
print(L)
copy
Output:
[(1, 10), (1, 20), (2, 10), (2, 20)] [Finished in 0.009557362645864487s]

With a custom repeat value

In the following example we specify a custom value for the repeat argument. 

ExampleEdit & Run
from itertools import product

data1 = [1, 2]
data2 = [10, 20]

print(*product(data1, data2, repeat = 2), sep = '\n')
copy
Output:
(1, 10, 1, 10) (1, 10, 1, 20) (1, 10, 2, 10) (1, 10, 2, 20) (1, 20, 1, 10) (1, 20, 1, 20) (1, 20, 2, 10) (1, 20, 2, 20) (2, 10, 1, 10) (2, 10, 1, 20) (2, 10, 2, 10) (2, 10, 2, 20) (2, 20, 1, 10) (2, 20, 1, 20) (2, 20, 2, 10) (2, 20, 2, 20) [Finished in 0.009799982886761427s]

In the above example, we used 2 as the repeat value, this means that 

Itertools.product() vs nested loops

Note that the itertools.product() function works just like nested loops, it is only that its syntax is much more concise and convenient than using for loops for the same purpose.

For example product(A, B) is equivalent to ((x,y) for x in A for y in B)

Consider the following examples:

ExampleEdit & Run

with itertools.product()

from itertools import product

data1 = [1, 2]
data2 = [10, 20]

print(*product(data1, data2))
copy
Output:
(1, 10) (1, 20) (2, 10) (2, 20) [Finished in 0.00976675609126687s]

The same example above can be written as follows using nested for loops

ExampleEdit & Run

with for loops

data1 = [1, 2]
data2 = [10, 20]

for i in data1:
   for j in data2:
      print((i, j))
copy
Output:
(1, 10) (1, 20) (2, 10) (2, 20) [Finished in 0.00981135107576847s]

And it can even be written more concisely using the list comprehension syntax as shown below:

ExampleEdit & Run
data1 = [1, 2]
data2 = [10, 20]

result = [(i, j) for i in data1 for j in data2]
print(result)
copy
Output:
[(1, 10), (1, 20), (2, 10), (2, 20)] [Finished in 0.00977128790691495s]

itertools.product() hackerrank solution

Hackerrank has a problem on itertools.product which is stated as follows:

Task

You are given a two lists A and B. Your task is to compute their cartesian product A X B.

Example

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

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

Note:  and  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 .
The second line contains the space separated elements of list .

Both lists have no duplicate integer elements.

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)

Solution

ExampleEdit & Run
from itertools import product
 
list1 = [int(i) for i in input().split()]
list2 = [int(i) for i in input().split()]
 
cart_product = product(list1,list2)
print(*cart_product)
copy
Output:
(1, 3) (1, 4) (2, 3) (2, 4)

In the above example;

  1. The input() function is called twice inside of a list comprehension. we expect space separated values to be entered as inputs.
  2. The space separated values entered are converted into integer before being added into list1 or list2.
  3. We then calculate the cartesion product of list1 and list2 using the product() function.
  4. Finally, we print all the values in the returned iterator by unpacking them to the print() function, i.e print(*cart_product)