Python program to apply itertools.product to elements of a list of lists


The itertools is a module in the Python standard library that provides a collection of tools for efficient iteration and combination of iterables and as it is part of python standard library no need to perform any additional installations. It offers various functions that can be used to manipulate, combine, and iterate over iterables in different ways.

The itertools.product() function is related to the itertools module, which is powerful tool for generating the cartesian product of multiple iterables. It takes one or more iterables as input and returns an iterator that produces tuples representing all possible combinations of the elements from the input iterables.

The itertools.product returns an iterator; in other words, it generates the tuples on the fly as they are requested. This is memory-efficient, especially when dealing with large input iterables or when the number of combinations is huge.

Syntax

The syntax for using itertools.product() function is as follows –

itertools.product(*iterables, repeat=1)

Where,

  • iterables are one or more elements combined to generate the Cartesian product.

  • repeat is an integer value that specifies the number of times each element in the input iterables should be repeated.

Example

In this example, we are using itertools.product() to generate all possible combinations of colors and sizes. The resulting iterator product contains tuples representing each combination, and we iterate over it to print each tuple.

import itertools
colors = ['red', 'green', 'blue']
sizes = ['S', 'M', 'L']
product = itertools.product(colors, sizes)
for item in product:
   print(item)

Output

('red', 'S')
('red', 'M')
('red', 'L')
('green', 'S')
('green', 'M')
('green', 'L')
('blue', 'S')
('blue', 'M')
('blue', 'L')

Example

In this example, we are trying to compute the Cartesian product of the letters and numbers lists by using the itertools.product() function then the resulting iterator generates tuples with all possible combinations of one element from letters and one element from numbers. The tuples are then printed using a loop.

import itertools
letters = ['a', 'b']
numbers = [1, 2, 3]
result = itertools.product(letters, numbers)
for item in result:
   print(item)

Output

('a', 1)
('a', 2)
('a', 3)
('b', 1)
('b', 2)
('b', 3)

Example

In the previous example we calculated the Cartesian product of numbers and letters with the repetition of one time. Here in this example, the repeat parameter is set to 3, which causes the Cartesian product to be repeated three times and the resulting iterator generates tuples with all possible combinations of three elements from the numbers list, including repetitions.

import itertools
letters = ['a', 'b']
numbers = [1, 2, 3]
result = itertools.product(letters, numbers, repeat = 2)
for item in result:
   print(item)

Output

('a', 1, 'a', 1)
('a', 1, 'a', 2)
('a', 1, 'a', 3)
('a', 1, 'b', 1)
('a', 1, 'b', 2)
('a', 1, 'b', 3)
('a', 2, 'a', 1)
('a', 2, 'a', 2)
('a', 2, 'a', 3)
('a', 2, 'b', 1)
('a', 2, 'b', 2)
('a', 2, 'b', 3)
('a', 3, 'a', 1)
('a', 3, 'a', 2)
('a', 3, 'a', 3)
('a', 3, 'b', 1)
('a', 3, 'b', 2)
('a', 3, 'b', 3)
('b', 1, 'a', 1)
('b', 1, 'a', 2)
('b', 1, 'a', 3)
('b', 1, 'b', 1)
('b', 1, 'b', 2)
('b', 1, 'b', 3)
('b', 2, 'a', 1)
('b', 2, 'a', 2)
('b', 2, 'a', 3)
('b', 2, 'b', 1)
('b', 2, 'b', 2)
('b', 2, 'b', 3)
('b', 3, 'a', 1)
('b', 3, 'a', 2)
('b', 3, 'a', 3)
('b', 3, 'b', 1)
('b', 3, 'b', 2)
('b', 3, 'b', 3)

Updated on: 02-Aug-2023

116 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements