Python Itertools.permutations() 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.permutations() Function:

Itertools.permutations() function is part of the Combinatoric Generators class. Combinatoric iterators are recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products.

The term “permutation” refers to all of the possible ways in which a set or string can be ordered or arranged. Similarly, itertool. The permutations() method returns all possible arrangements for an iterator, and all elements are assumed to be unique based on their position rather than their value or category.

All of these permutations are listed in alphabetical order. The function itertools.permutations() accepts an iterator and ‘r’ (length of permutation required) as input and, if not specified, assumes ‘r’ as the default length of the iterator and returns all possible permutations of length ‘r’ each.

Syntax:

permutations(iterator, r)

Parameters

iterator: This is Required. Elements whose cartesian product must be calculated.

r: This is Optional. The length of the outputs (number of iterables by default).

Examples:

Example1:

Input:

Given string = "hello"
Given r value(length)= 3

Output:

('h', 'e', 'l')
('h', 'e', 'l')
('h', 'e', 'o')
('h', 'l', 'e')
('h', 'l', 'l')
('h', 'l', 'o')
('h', 'l', 'e')
('h', 'l', 'l')
('h', 'l', 'o')
('h', 'o', 'e')
('h', 'o', 'l')
('h', 'o', 'l')
('e', 'h', 'l')
('e', 'h', 'l')
('e', 'h', 'o')
('e', 'l', 'h')
('e', 'l', 'l')
('e', 'l', 'o')
('e', 'l', 'h')
('e', 'l', 'l')
('e', 'l', 'o')
('e', 'o', 'h')
('e', 'o', 'l')
('e', 'o', 'l')
('l', 'h', 'e')
('l', 'h', 'l')
('l', 'h', 'o')
('l', 'e', 'h')
('l', 'e', 'l')
('l', 'e', 'o')
('l', 'l', 'h')
('l', 'l', 'e')
('l', 'l', 'o')
('l', 'o', 'h')
('l', 'o', 'e')
('l', 'o', 'l')
('l', 'h', 'e')
('l', 'h', 'l')
('l', 'h', 'o')
('l', 'e', 'h')
('l', 'e', 'l')
('l', 'e', 'o')
('l', 'l', 'h')
('l', 'l', 'e')
('l', 'l', 'o')
('l', 'o', 'h')
('l', 'o', 'e')
('l', 'o', 'l')
('o', 'h', 'e')
('o', 'h', 'l')
('o', 'h', 'l')
('o', 'e', 'h')
('o', 'e', 'l')
('o', 'e', 'l')
('o', 'l', 'h')
('o', 'l', 'e')
('o', 'l', 'l')
('o', 'l', 'h')
('o', 'l', 'e')
('o', 'l', 'l')

Example2:

Input:

Given list = [123, 'hello', 'all']
Given r value(length)= 3

Output:

The all given list's permutations are :
[(123, 'hello', 'all'), (123, 'all', 'hello'), ('hello', 123, 'all'), ('hello', 'all', 123), ('all', 123, 'hello'), ('all', 'hello', 123)]

itertools.permutations() Function with Examples in Python

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

Approach:

  • Import permutations() 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 permutations() function and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Loop in the above result list using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import permutations() function from itertools module using the
# import keyword.
from itertools import permutations
# 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 = 3
# Pass the given string and 'r' value as the arguments to the
# permutations() function and store it in a variable.
rslt = permutations(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)
# Loop in the above result list using the for loop
for itr in rslt_lst:
    # Inside the loop, print the iterator value
    print(itr)

Output:

('h', 'e', 'l')
('h', 'e', 'l')
('h', 'e', 'o')
('h', 'l', 'e')
('h', 'l', 'l')
('h', 'l', 'o')
('h', 'l', 'e')
('h', 'l', 'l')
('h', 'l', 'o')
('h', 'o', 'e')
('h', 'o', 'l')
('h', 'o', 'l')
('e', 'h', 'l')
('e', 'h', 'l')
('e', 'h', 'o')
('e', 'l', 'h')
('e', 'l', 'l')
('e', 'l', 'o')
('e', 'l', 'h')
('e', 'l', 'l')
('e', 'l', 'o')
('e', 'o', 'h')
('e', 'o', 'l')
('e', 'o', 'l')
('l', 'h', 'e')
('l', 'h', 'l')
('l', 'h', 'o')
('l', 'e', 'h')
('l', 'e', 'l')
('l', 'e', 'o')
('l', 'l', 'h')
('l', 'l', 'e')
('l', 'l', 'o')
('l', 'o', 'h')
('l', 'o', 'e')
('l', 'o', 'l')
('l', 'h', 'e')
('l', 'h', 'l')
('l', 'h', 'o')
('l', 'e', 'h')
('l', 'e', 'l')
('l', 'e', 'o')
('l', 'l', 'h')
('l', 'l', 'e')
('l', 'l', 'o')
('l', 'o', 'h')
('l', 'o', 'e')
('l', 'o', 'l')
('o', 'h', 'e')
('o', 'h', 'l')
('o', 'h', 'l')
('o', 'e', 'h')
('o', 'e', 'l')
('o', 'e', 'l')
('o', 'l', 'h')
('o', 'l', 'e')
('o', 'l', 'l')
('o', 'l', 'h')
('o', 'l', 'e')
('o', 'l', 'l')

Note:

If no length(r value) is specified, the default value will be taken as 
string length

Similarly, try for the list, string, and range as shown below

from itertools import permutations

print("The all given list's permutations are :")
print(list(permutations([123, 'hello', 'all'], 3)))
print()

print("The all given strings's permutations are :")
print(list(permutations('hi')))
print()

print("The all given container's permutations are :")
print(list(permutations(range(4), 3)))

Output:

The all given list's permutations are :
[(123, 'hello', 'all'), (123, 'all', 'hello'), ('hello', 123, 'all'), ('hello', 'all', 123), ('all', 123, 'hello'), ('all', 'hello', 123)]

The all given strings's permutations are :
[('h', 'i'), ('i', 'h')]

The all given container's permutations are :
[(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0, 3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)]

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

Approach:

  • Import permutations() 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 permutations() function and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Loop in the above result list using the for loop.
  • Inside the loop, print the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Import permutations() function from itertools module using the
# import keyword.
from itertools import permutations
# 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
# permutations() function and store it in a variable.
rslt = permutations(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)
# Loop in the above result list using the for loop
for itr in rslt_lst:
    # Inside the loop, print the iterator value
    print(itr)

Output:

Enter some random string = good
Enter some random number = 3
('g', 'o', 'o')
('g', 'o', 'd')
('g', 'o', 'o')
('g', 'o', 'd')
('g', 'd', 'o')
('g', 'd', 'o')
('o', 'g', 'o')
('o', 'g', 'd')
('o', 'o', 'g')
('o', 'o', 'd')
('o', 'd', 'g')
('o', 'd', 'o')
('o', 'g', 'o')
('o', 'g', 'd')
('o', 'o', 'g')
('o', 'o', 'd')
('o', 'd', 'g')
('o', 'd', 'o')
('d', 'g', 'o')
('d', 'g', 'o')
('d', 'o', 'g')
('d', 'o', 'o')
('d', 'o', 'g')
('d', 'o', 'o')