Python Itertools.combinations() Function with Examples

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 space efficiency. itertools enable us to solve complex problems quickly and easily. Iterators are classified into three types.

This module provides the following types of iterators:

  1. Combinatorics Generators
  2. Infinite Iterators
  3. Terminating Iterators

itertools.combinations() Function:

Generate and print all possible combinations of r elements in an array of size 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.

Syntax:

itertools.combinations(iterable, r)

Parameters

iterable: This is Required. Elements whose combinations must be performed out.

r: This is Required. It is the length of the outputs.

Using itertools.combinations(iterable, r):
It returns sorted r-length tuples with no repeated elements.

for example:

combinations(‘ABCD’, 2) = [AB, AC, AD, BC, BD, CD].

Using combinations_with_replacement(iterable, r):
It returns sorted r-length tuples with repeated elements.

for example:

combinations_with_replacement(‘ABCD’, 2),  = [AA, AB, AC, AD, BB, BC, BD, CC, CD, DD].

Examples:

Example1:

Input:

Given list  = [5, 6, 7, 8]
Given r value =  2

Output:

The possible combinations are : 
[(5, 6), (5, 7), (5, 8), (6, 7), (6, 8), (7, 8)]

Example2:

Input:

Given list  = [10, 50, 100, 40]
Given r value =  3

Output:

The possible combinations are : 
[(10, 50, 100), (10, 50, 40), (10, 100, 40), (50, 100, 40)]

itertools.combinations() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import combinations() function from itertools module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Give the r value as static input and store it in another variable.
  • Pass the given list and ‘r’ value as the arguments to the combinations() function to get all the possible combinations of list elements for the given r value and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import combinations() function from itertools module using the
# import keyword.
from itertools import combinations
# Give the list as static input and store it in a variable.
gvn_lst = [5, 6, 7, 8]
# Give the r value as static input and store it in another variable.
gvn_r_valu = 2
# Pass the given list and 'r' value as the arguments to the combinations()
# function to get all the possible combinations of list elements for the given
# r value and store it in a variable.
rslt = combinations(gvn_lst, gvn_r_valu)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The possible combinations are : ")
print(rslt_lst)

Output:

The possible combinations are : 
[(5, 6), (5, 7), (5, 8), (6, 7), (6, 8), (7, 8)]

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import combinations() function from itertools module using the import keyword.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the r value as user input using the int(input()) function and store it in another variable.
  • Pass the given list and ‘r’ value as the arguments to the combinations() function to get all the possible combinations of list elements for the given r value and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import combinations() function from itertools module using the
# import keyword.
from itertools import combinations
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
   
# Give the r value as user input using the int(input()) function and
# store it in another variable.
gvn_r_valu = int(input("Enter some random number = "))
# Pass the given list and 'r' value as the arguments to the combinations()
# function to get all the possible combinations of list elements for the given
# r value and store it in a variable.
rslt = combinations(gvn_lst, gvn_r_valu)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The possible combinations are : ")
print(rslt_lst)

Output:

Enter some random List Elements separated by spaces = 10 50 100 40
Enter some random number = 3
The possible combinations are : 
[(10, 50, 100), (10, 50, 40), (10, 100, 40), (50, 100, 40)]