{"id":25607,"date":"2021-11-23T08:54:12","date_gmt":"2021-11-23T03:24:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=25607"},"modified":"2021-11-23T08:54:12","modified_gmt":"2021-11-23T03:24:12","slug":"python-itertools-combinations-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-combinations-function-with-examples\/","title":{"rendered":"Python Itertools.combinations() 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.combinations() Function:<\/strong><\/p>\n Generate and print all possible combinations of r elements in an array of size n.<\/p>\n It returns r length subsequences of the input iterable’s elements. Combinations are emitted in alphabetical order. As a result, if the input iterable is sorted, the combination tuples will be generated in the same order.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n iterable:<\/strong>\u00a0This is Required. Elements whose combinations must be performed out.<\/p>\n r:<\/strong>\u00a0This is Required. It is the length of the outputs.<\/p>\n Using itertools.combinations(iterable, r):<\/strong> for example:<\/p>\n combinations(‘ABCD’, 2) = [AB, AC, AD, BC, BD, CD].<\/p>\n Using combinations_with_replacement(iterable, r):<\/strong> for example:<\/p>\n combinations_with_replacement(‘ABCD’, 2),\u00a0 = [AA, AB, AC, AD, BB, BC, BD, CC, CD, DD].<\/span><\/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 <\/p>\n","protected":false},"excerpt":{"rendered":" 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
itertools.combinations(iterable, r)<\/pre>\n
\nIt returns sorted r-length tuples with no repeated elements.<\/p>\n
\nIt returns sorted r-length tuples with repeated elements.<\/p>\nGiven list\u00a0 = [5, 6, 7, 8]\r\nGiven r value = 2<\/pre>\n
The possible combinations are : \r\n[(5, 6), (5, 7), (5, 8), (6, 7), (6, 8), (7, 8)]<\/pre>\n
Given list\u00a0 = [10, 50, 100, 40]\r\nGiven r value = 3<\/pre>\n
The possible combinations are : \r\n[(10, 50, 100), (10, 50, 40), (10, 100, 40), (50, 100, 40)]<\/pre>\n
itertools.combinations() Function with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import combinations() function from itertools module using the\r\n# import keyword.\r\nfrom itertools import combinations\r\n# Give the list as static input and store it in a variable.\r\ngvn_lst = [5, 6, 7, 8]\r\n# Give the r value as static input and store it in another variable.\r\ngvn_r_valu = 2\r\n# Pass the given list and 'r' value as the arguments to the combinations()\r\n# function to get all the possible combinations of list elements for the given\r\n# r value and store it in a variable.\r\nrslt = combinations(gvn_lst, gvn_r_valu)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(\"The possible combinations are : \")\r\nprint(rslt_lst)\r\n<\/pre>\n
The possible combinations are : \r\n[(5, 6), (5, 7), (5, 8), (6, 7), (6, 8), (7, 8)]<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import combinations() function from itertools module using the\r\n# import keyword.\r\nfrom itertools import combinations\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n \r\n# Give the r value as user input using the int(input()) function and\r\n# store it in another variable.\r\ngvn_r_valu = int(input(\"Enter some random number = \"))\r\n# Pass the given list and 'r' value as the arguments to the combinations()\r\n# function to get all the possible combinations of list elements for the given\r\n# r value and store it in a variable.\r\nrslt = combinations(gvn_lst, gvn_r_valu)\r\n# Convert the above result into a list using the list() function and store it in\r\n# another variable.\r\nrslt_lst = list(rslt)\r\n# Print the above result.\r\nprint(\"The possible combinations are : \")\r\nprint(rslt_lst)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 50 100 40\r\nEnter some random number = 3\r\nThe possible combinations are : \r\n[(10, 50, 100), (10, 50, 40), (10, 100, 40), (50, 100, 40)]<\/pre>\n