The Wayback Machine - https://web.archive.org/web/20240823122737/https://www.geeksforgeeks.org/python-itertools-product/
Open In App

Python – Itertools.Product()

Last Updated : 29 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In the terms of Mathematics Cartesian Product of two sets is defined as the set of all ordered pairs (a, b) where a belongs to A and b belongs to B. Consider the below example for better understanding.
Examples:

Input : arr1 = [1, 2, 3] 
arr2 = [5, 6, 7] 
Output : [(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)] 
Input : arr1 = [10, 12] 
arr2 = [8, 9, 10] 
Output : [(10, 8), (10, 9), (10, 10), (12, 8), (12, 9), (12, 10)] 
 


The above solution can be done by looping but we will use a special Python library itertools.product() for finding the Cartesian Product. Let’s go through the working and use cases of this Python library.
 

What are Itertools in Python?


Python Itertools is a library in Python which consists of multiple methods that are used in various iterators to compute a fast and code efficient solution.
 

itertools.product() falls under the category called Combinatoric iterators of the Python itertools library.


Note: For more information, refer to Python Itertools
 

What does itertools.product() do?

itertools.product() is used to find the cartesian product from the given iterator, output is lexicographic ordered. The itertools.product() can used in two different ways:

  • itertools.product(*iterables, repeat=1):
    It returns the cartesian product of the provided iterable with itself for the number of times specified by the optional keyword “repeat”. For example, product(arr, repeat=3) means the same as product(arr, arr, arr).
  • itertools.product(*iterables):
    It returns the cartesian product of all the iterable provided as the argument. For example, product(arr1, arr2, arr3).


Example:

Python3
from itertools import product

def cartesian_product(arr1, arr2):

    # return the list of all the computed tuple
    # using the product() method
    return list(product(arr1, arr2)) 
  
# Driver Function 
if __name__ == "__main__": 
    arr1 = [1, 2, 3]
    arr2 = [5, 6, 7]
    print(cartesian_product(arr1, arr2))

Output:

[(1, 5), (1, 6), (1, 7), (2, 5), (2, 6), (2, 7), (3, 5), (3, 6), (3, 7)] 


Previous Article
Next Article

Similar Reads

Python program to apply itertools.product to elements of a list of lists
Itertools is a module that consists of the methods to apply various iteration based operations including combinations, permutations, etc., on the iterable components in Python. It has a set lightweight, memory-efficient and fast tools for performing iterator algebra. Note: For more information, refer to Python Itertools itertools.product() It is us
2 min read
Print first n distinct permutations of string using itertools in Python
Given a string with duplicate characters allowed, print first n permutations of given string such that no permutation is repeated. Examples: Input : string = "abcab", n = 10 Output : aabbc aabcb aacbb ababc abacb abbac abbca abcab abcba acabb Input : string = "okok", n = 4 Output : kkoo koko kook okko Approach: Python provides an inbuilt method to
2 min read
Python Itertools
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. For example, let's suppose there are two lists and you want to multiply their elements. There can be sever
13 min read
itertools.groupby() in Python
Prerequisites: Python Itertools Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Itertools.groupby() This method calculates the keys for each element prese
2 min read
Python - itertools.repeat()
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools repeat() itertools.repeat()falls under the category
2 min read
Python - Itertools.takewhile()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easy. One such itertools function is takewhile(). Note: For more information, refer to Python Itertools takewhile() This allows considering an item from the iterable until t
3 min read
Python - Itertools.compress()
Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Compress() The itertools.compress() falls under the
2 min read
Python - Itertools.dropwhile()
Itertools is a Python module that provide various functions that work on iterators to produce complex iterators. It makes the code faster, memory efficient and thus we see a better performance. This module is either used by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools Dropwhile() The d
1 min read
Python - Itertools.cycle()
Iterator is defined as object types which contains values that can be accessed or iterated using a loop. There are different iterators that come built-in with Python such as lists, sets, etc. Itertools is the Python module that contains some inbuilt functions for generating sequences using iterators. This module provides various functions that work
3 min read
Python - Itertools.chain()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is chain().Note: For more information, refer to Python Itertools chain() function It is a function that takes a series of iterables and r
4 min read
Python - Itertools.starmap()
The itertools is a module in Python having a collection of functions that are used for handling iterators. They make iterating through the iterables like lists and strings very easily. One such itertools function is starmap().Note: For more information, refer to Python Itertools starmap() function When an iterable is contained within another iterab
3 min read
Python - Itertools.Permutations()
Itertool is a module provided by Python for creating iterators for efficient looping. It also provides various features or functions that work with iterators to produce complex iterators and help us to solve problems easily and efficiently in terms of time as well as memory. Itertools module provides us various ways to manipulate the sequence that
3 min read
Python - Itertools Combinations() function
Itertool is a module of Python which is used to creation of iterators which helps us in efficient looping in terms of space as well as time. This module helps us to solve complex problems easily with the help of different sub-functions of itertools. The different sub-functions are divided into 3 subgroups which are:- Infinite IteratorsIterators ter
2 min read
Combinations in Python without using itertools
Itertools in Python is a module that produces complex iterators with the help of methods that work on iterators. This module works as a fast, memory-efficient tool that is used either by itself or in combination to form iterator algebra. Printing Combinations Using itertoolsUsing Itertools we can display all the possible combinations of the string
3 min read
Python - Itertools.zip_longest()
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Iterators in Python is an object that can iterate like sequence data types such as list, tuple, str and so
2 min read
Python - Itertools.islice()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is islice(). Note: For more information, refer to Python Itertools islice() function This iterator selectively prints the values mentioned in
2 min read
Python - Itertools.accumulate()
Python itertools module is a collection of tools for handling iterators. According to the official documentation: "Module [that] implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SMLâ€Ķ Together, they form an ‘iterator algebra’ making it possible to construct specialized tools succinctly and efficiently in
4 min read
Python - Itertools.count()
Python Itertools are a great way of creating complex iterators which helps in getting faster execution time and writing memory-efficient code. Itertools provide us with functions for creating infinite sequences and itertools.count() is one such function and it does exactly what it sounds like, it counts! Note: For more information, refer to Python
3 min read
Python - Itertools.Combinations_with_replacement()
Itertools in Python refers to module provided in Python for the creation of iterators which helps in efficient looping, time and space efficiency as well. Itertools helps us to solve complex problems easily and efficiently. There are in general 3 types of iterators.Different types of iterators provided by this module are: Combinatoric Generators In
2 min read
Python - Itertools.chain.from_iterable()
Python's Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools The functions under itertools can be classified into
2 min read
Python - Itertools.filterfalse()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is filterfalse(). Note: For more information, refer to Python Itertools filterfalse() function This iterator prints only values that return f
2 min read
Python - Itertools.tee()
In Python, Itertools is the inbuilt module that allows us to handle the iterators in an efficient way. They make iterating through the iterables like lists and strings very easily. One such itertools function is tee(). Note: For more information, refer to Python Itertools tee() function This iterator splits the container into a number of iterators
2 min read
itertools.combinations() module in Python to print all possible combinations
Given an array of size n, generate and print all possible combinations of r elements in array. Examples: Input : arr[] = [1, 2, 3, 4], r = 2Output : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]This problem has existing recursive solution please refer Print all possible combinations of r elements in a given array of size n link. We will solve th
2 min read
Itertools in Python3
Itertools is a module in Python, it is used to iterate over data structures that can be stepped over using a for-loop. Such data structures are also known as iterables. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Why to use ? This module incorporates functions that
3 min read
Flipkart Product Price Tracker using Python
Flipkart Private Limited is an Indian e-commerce company. It sells many products and the prices of these products keep changing. Generally, during sales and festivals, the price of products drops, and our aim is to buy any product at the lowest possible price. In this article, we will learn to build a Flipkart product price tracker using python to
3 min read
Python | Pandas series.cumprod() to find Cumulative product of a Series
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas Series.cumprod() is used to find Cumulative product of a series. In cumulative product, the length of returned series is same as
3 min read
Python | Pandas dataframe.product()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.product() function return the value of the product for the requested axis. It multiplies all the element together on th
2 min read
Python | Pandas Series.product()
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.product() function returns the product of the underlying data in the given Seri
3 min read
Amazon product availability checker using Python
As we know Python is a multi-purpose language and widely used for scripting. Its usage is not just limited to solve complex calculations but also to automate daily life task. Let’s say we want to track any Amazon product availability and grab the deal when the product availability changes and inform the user of availability through email. It will b
3 min read
Python | Cumulative Nested Tuple Column Product
Sometimes, while working with records, we can have a problem in which we require to perform index wise multiplication of tuple elements. This can get complicated with tuple elements to be tuple and inner elements again be tuple. Let’s discuss certain ways in which this problem can be solved. Method #1 : Using zip() + nested generator expression The
7 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg