Python Itertools.combinations_with_replacement() 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 enables 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_with_replacement() Function:

itertools.combinations_with_replacement() function belongs to the itertools Combinatoric Generator subtype. Combinatoric generators are iterators that deal with the various arrangements that an iterator can take. The elements are referred to here by their index value rather than their value or type.

Usage of this function

The term “combinations” refers to all of the possible subsets or arrangements of the iterator, whereas “combinations with replacement” refers to all of the possible arrangements or subsets that allow an element to repeat in a subset.

This function accepts ‘r’ as an input, where ‘r’ represents the number of possible combinations. All combinations with element repetition are emitted and have a length of ‘r,’ and ‘r’ is a necessary argument here.

Syntax:

itertools.combinations_with_replacement(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.

Examples:

Example1:

Input:

Given string  = "hello"
Given r value =  2

Output:

The possible combinations with replacemenet are : 
[('h', 'h'), ('h', 'e'), ('h', 'l'), ('h', 'l'), ('h', 'o'), ('e', 'e'), ('e', 'l'), ('e', 'l'), ('e', 'o'), ('l', 'l'), ('l', 'l'), ('l', 'o'), ('l', 'l'), ('l', 'o'), ('o', 'o')]

Example2:

Input:

Given string  = "good"
Given r value = 3

Output:

The possible combinations with replacemenet are : 
[('g', 'g', 'g'), ('g', 'g', 'o'), ('g', 'g', 'o'), ('g', 'g', 'd'), ('g', 'o', 'o'), ('g', 'o', 'o'), ('g', 'o', 'd'), ('g', 'o', 'o'), ('g', 'o', 'd'), ('g', 'd', 'd'), ('o', 'o', 'o'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'd', 'd'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'd', 'd'), ('d', 'd', 'd')]

itertools.combinations_with_replacement() Function with Examples in Python

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

Approach:

  • Import combinations_with_replacement() function from itertools module using the import keyword.
  • Give the string 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 string and ‘r’ value as the arguments to the combinations_with_replacement() function 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_with_replacement() function from itertools module using the
# import keyword.
from itertools import combinations_with_replacement
# Give the string as static input and store it in a variable.
gvn_str = "hello"
# Give the r value as static input and store it in another variable.
gvn_r_valu = 2
# Pass the given string and 'r' value as the arguments to the
# combinations_with_replacement() function and store it in a variable.
rslt = combinations_with_replacement(gvn_str, 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 with repalcement are : ")
print(rslt_lst)

Output:

The possible combinations with repalcement are : 
[('h', 'h'), ('h', 'e'), ('h', 'l'), ('h', 'l'), ('h', 'o'), ('e', 'e'), ('e', 'l'), ('e', 'l'), ('e', 'o'), ('l', 'l'), ('l', 'l'), ('l', 'o'), ('l', 'l'), ('l', 'o'), ('o', 'o')]

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

Approach:

  • Import combinations_with_replacement() function from itertools module using the import keyword.
  • Give the string as user input using the input() function and 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 string and ‘r’ value as the arguments to the combinations_with_replacement() function 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_with_replacement() function from itertools module using the
# import keyword.
from itertools import combinations_with_replacement
# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# 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 string and 'r' value as the arguments to the
# combinations_with_replacement() function and store it in a variable.
rslt = combinations_with_replacement(gvn_str, 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 string = good
Enter some random number = 3
The possible combinations are : 
[('g', 'g', 'g'), ('g', 'g', 'o'), ('g', 'g', 'o'), ('g', 'g', 'd'), ('g', 'o', 'o'), ('g', 'o', 'o'), ('g', 'o', 'd'), ('g', 'o', 'o'), ('g', 'o', 'd'), ('g', 'd', 'd'), ('o', 'o', 'o'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'd', 'd'), ('o', 'o', 'o'), ('o', 'o', 'd'), ('o', 'd', 'd'), ('d', 'd', 'd')]