From itertools import product – Python Itertools.product()

itertools:

From itertools import product: itertools is a Python package that provides several functions that work on iterators to generate complicated iterators. This module functions as a fast, memory-efficient tool that can be used alone or in combination to construct iterator algebra.

The key feature of itertools is that its functions are utilized to generate memory-efficient and exact code.

In this article, we will look at itertools. product(), which is a combinatorics generator. This tool is used to calculate the Cartesian Product of two sets and to identify all possible pairs in the form of a tuple (x,y), where x belongs to one set and y belongs to another. Cartesian products can also be achieved with a for loop, although they are less efficient and more difficult to code than itertools.

Examples:

Example1:

Input:

Given First List =[4, 19, 11, 5]
Given Second List=[10, 9, 8, 6, 5, 3]

Output:

The Cartesian Product of the two lists : 
[(4, 10), (4, 9), (4, 8), (4, 6), (4, 5), (4, 3), (19, 10), (19, 9), (19, 8), (19, 6), (19, 5), (19, 3), (11, 10), (11, 9), (11, 8), (11, 6), 
(11, 5), (11, 3), (5, 10), (5, 9), (5, 8), (5, 6), (5, 5), (5, 3)]

Example2:

Input:

Given First List=['hello', 'this', 'is', 'btechgeeks']
Given Second List =['sky', 'is', 'blue']

Output:

The Cartesian Product of the two lists : 
[('hello', 'sky'), ('hello', 'is'), ('hello', 'blue'), ('this', 'sky'), ('this', 'is'), ('this', 'blue'), ('is', 'sky'), ('is', 'is'), ('is', 'blue'), 
('btechgeeks', 'sky'), ('btechgeeks', 'is'), ('btechgeeks', 'blue')]

Itertools.product() in Python

Python itertools.product: Below are the ways to implement Itertools. product() in Python.

Method #1: Using Itertools.product() (Static Input)

I)Number lists

Approach:

  • Import product from itertools using the import keyword.
  • Give the first list as static input and store it in a variable.
  • Give the second list as static input and store it in another variable.
  • We use the product function to create a list, which we then print.
  • Print the Product of the two lists.
  • The Exit of the Program.

Below is the implementation:

# Import product from itertools using the import keyword.
from itertools import product
# Give the first list as static input and store it in a variable.
lstt1 = [4, 19, 11, 5]
# Give the second list as static input and store it in another variable.
lstt2 = [10, 9, 8, 6, 5, 3]
# We use the product function to create a list, which we then print.
productlists = list(product(lstt1, lstt2))
# Print the Product of the two lists.
print('The Cartesian Product of the two lists : ')
print(productlists)

Output:

The Cartesian Product of the two lists : 
[(4, 10), (4, 9), (4, 8), (4, 6), (4, 5), (4, 3), (19, 10), (19, 9), (19, 8), (19, 6), (19, 5), (19, 3), (11, 10), (11, 9), (11, 8), (11, 6), 
(11, 5), (11, 3), (5, 10), (5, 9), (5, 8), (5, 6), (5, 5), (5, 3)]

II)String lists

Approach:

  • Import product from itertools using the import keyword.
  • Give the first list(string list) as static input and store it in a variable.
  • Give the second list(string list) as static input and store it in another variable.
  • We use the product function to create a list, which we then print.
  • Print the Product of the two lists.
  • The Exit of the Program.

Below is the implementation:

# Import product from itertools using the import keyword.
from itertools import product
# Give the first list(string list) as static input and store it in a variable.
lstt1 = ['hello', 'this', 'is', 'btechgeeks']
# Give the second list(string list) as static input and store it in another variable.
lstt2 = ['sky', 'is', 'blue']
# We use the product function to create a list, which we then print.
productlists = list(product(lstt1, lstt2))
# Print the Product of the two lists.
print('The Cartesian Product of the two lists : ')
print(productlists)

Output:

The Cartesian Product of the two lists : 
[('hello', 'sky'), ('hello', 'is'), ('hello', 'blue'), ('this', 'sky'), ('this', 'is'), ('this', 'blue'), ('is', 'sky'), ('is', 'is'), ('is', 'blue'), 
('btechgeeks', 'sky'), ('btechgeeks', 'is'), ('btechgeeks', 'blue')]

Method #2: Using Itertools.product() (User Input)

I)Number lists

Approach:

  • Import product from itertools using the import keyword.
  • Give the first list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • We use the product function to create a list, which we then print.
  • Print the Product of the two lists.
  • The Exit of the Program.

Below is the implementation:

# Import product from itertools using the import keyword.
from itertools import product
# Give the first list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
lstt1 = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# Give the second list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
lstt2 = list(map(int, input(
    'Enter some random List Elements separated by spaces = ').split()))
# We use the product function to create a list, which we then print.
productlists = list(product(lstt1, lstt2))
# Print the Product of the two lists.
print('The Cartesian Product of the two lists : ')
print(productlists)

Output:

Enter some random List Elements separated by spaces = 6 8 7 2 9
Enter some random List Elements separated by spaces = 11 12
The Cartesian Product of the two lists : 
[(6, 11), (6, 12), (8, 11), (8, 12), (7, 11), (7, 12), (2, 11), (2, 12), (9, 11), (9, 12)]

II)String lists

Approach:

  • Import product from itertools using the import keyword.
  • Give the first list as user input using list(),input(),and split() functions.
  • Store it in a variable.
  • Give the second list as user input using list(),input(),and split() functions.
  • Store it in a variable.
  • We use the product function to create a list, which we then print.
  • Print the Product of the two lists.
  • The Exit of the Program.

Below is the implementation:

# Import product from itertools using the import keyword.
from itertools import product
# Give the first list as user input using list(),input(),and split() functions.
# Store it in a variable.
lstt1 = list(input(
    'Enter some random List Elements separated by spaces = ').split())
# Give the second list as user input using list(),input(),and split() functions.
# Store it in a variable.
lstt2 = list(input(
    'Enter some random List Elements separated by spaces = ').split())
# We use the product function to create a list, which we then print.
productlists = list(product(lstt1, lstt2))
# Print the Product of the two lists.
print('The Cartesian Product of the two lists : ')
print(productlists)

Output:

Enter some random List Elements separated by spaces = good morning this is btechgeeks
Enter some random List Elements separated by spaces = how are you
The Cartesian Product of the two lists : 
[('good', 'how'), ('good', 'are'), ('good', 'you'), ('morning', 'how'), ('morning', 'are'), ('morning', 'you'), ('this', 'how'), 
('this', 'are'), ('this', 'you'), ('is', 'how'), ('is', 'are'), ('is', 'you'), ('btechgeeks', 'how'), ('btechgeeks', 'are'),
 ('btechgeeks', 'you')]

Related Programs: