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

product(*iterables, repeat = 1)
*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.

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.

from itertools import product

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

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

print(*result)

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:

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))

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

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)

With a custom repeat value

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

from itertools import product

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

print(*product(data1, data2, repeat = 2), sep = '\n')

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:

with itertools.product()

from itertools import product

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

print(*product(data1, data2))

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

with for loops

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

for i in data1:
   for j in data2:
      print((i, j))

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

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

result = [(i, j) for i in data1 for j in data2]
print(result)

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

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)

(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)