{"id":25663,"date":"2021-12-04T09:18:55","date_gmt":"2021-12-04T03:48:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=25663"},"modified":"2021-12-04T09:18:55","modified_gmt":"2021-12-04T03:48:55","slug":"python-itertools-product-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-product-function-with-examples\/","title":{"rendered":"Python Itertools.product() Function with Examples"},"content":{"rendered":"
Itertools Module:<\/strong><\/p>\n Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.<\/p>\n Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and space efficiency. itertools enable us to solve complex problems quickly and easily. Iterators are classified into three types.<\/p>\n This module provides the following types of iterators:<\/p>\n itertools.product() Function:<\/strong><\/p>\n Cartesian Product of two sets is defined in mathematics as the set of all ordered pairs (a, b) where a belongs to A and b belongs to B.<\/p>\n itertools.product() belongs to the Python itertools library’s Combinatoric iterators category.<\/p>\n itertools.product() returns the cartesian product from the specified iterator, with the output being lexicographically sorted.<\/p>\n There are two ways to use itertools.product():<\/p>\n itertools.product(*iterables, repeat=1): <\/strong><\/p>\n This function returns the cartesian product of the provided iterable with itself for the number of times specified by the optional keyword “repeat.”<\/p>\n For example, product(arr, repeat=4) is equivalent to product (arr, arr, arr, arr). This function returns the cartesian product of all iterables passed as arguments. For instance, product (arr1, arr2, arr3, arr4).<\/p>\n Consider the following example to gain a better idea.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Itertools Module: Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings. Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and …<\/p>\n\n
\nitertools.product(*iterables):<\/strong><\/p>\nGiven first List = [10, 9, 8]\r\nGiven second List = [12, 3, 40]<\/pre>\n
The cartesian product of the given two lists = \r\n[(10, 12), (10, 3), (10, 40), (9, 12), (9, 3), (9, 40), (8, 12), (8, 3), (8, 40)]<\/pre>\n
Given first List = [78, 2, 6]\r\nGiven second List = [4, 2, 7]<\/pre>\n
The cartesian product of the given two lists = \r\n[(78, 4), (78, 2), (78, 7), (2, 4), (2, 2), (2, 7), (6, 4), (6, 2), (6, 7)]<\/pre>\n
Itertools.product() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import product() function from itertools module using the import keyword.\r\nfrom itertools import product\r\n\r\n# Create a function say CartesianProdct() which accepts the two lists as\r\n# the arguments and returns the cartesian Product of the given two lists\r\n\r\n\r\ndef CartesianProdct(gvn_fstlst, gvn_scndlst):\r\n\r\n # Using the product() method, return a list of all computed tuples.\r\n return list(product(gvn_fstlst, gvn_scndlst))\r\n\r\n\r\n# Inside the main function,\r\n# Give the first list as static input and store it in a variable.\r\ngvn_fstlst = [10, 9, 8]\r\n# Give the second list as static input and store it in another variable.\r\ngvn_scndlst = [12, 3, 40]\r\n# Pass the given two lists as the arguments to the above declared CartesianProdct()\r\n# function and store it in another variable.\r\nrslt = CartesianProdct(gvn_fstlst, gvn_scndlst)\r\n# Print the cartesian product of the given two lists\r\nprint(\"The cartesian product of the given two lists = \")\r\nprint(rslt)\r\n<\/pre>\n
The cartesian product of the given two lists = \r\n[(10, 12), (10, 3), (10, 40), (9, 12), (9, 3), (9, 40), (8, 12), (8, 3), (8, 40)]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import product() function from itertools module using the import keyword.\r\nfrom itertools import product\r\n\r\n# Create a function say CartesianProdct() which accepts the two lists as\r\n# the arguments and returns the cartesian Product of the given two lists\r\n\r\n\r\ndef CartesianProdct(gvn_fstlst, gvn_scndlst):\r\n\r\n # Using the product() method, return a list of all computed tuples.\r\n return list(product(gvn_fstlst, gvn_scndlst))\r\n\r\n\r\n# Inside the main function,\r\n# Give the first list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_fstlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the second list as user input using list(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\ngvn_scndlst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Pass the given two lists as the arguments to the above declared CartesianProdct()\r\n# function and store it in another variable.\r\nrslt = CartesianProdct(gvn_fstlst, gvn_scndlst)\r\n# Print the cartesian product of the given two lists\r\nprint(\"The cartesian product of the given two lists = \")\r\nprint(rslt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 78 2 6\r\nEnter some random List Elements separated by spaces = 4 2 7\r\nThe cartesian product of the given two lists = \r\n[(78, 4), (78, 2), (78, 7), (2, 4), (2, 2), (2, 7), (6, 4), (6, 2), (6, 7)]<\/pre>\n","protected":false},"excerpt":{"rendered":"