Python – Permutations and Combinations

This is a detailed tutorial on finding permutations and combinations of a sequence in Python using the itertools package in a single line of code.

Permutations and Combinations in Python

Manually finding all of the permutations and combinations of a sequence is a very difficult brainstorming task. And also there are very high chances of error while manually doing so. Python module itertools provides simple methods to find all the possible permutations and combinations of any sequence.

The while loops are good for basic iterations but itertools provide some special methods that can simplify our tasks of complex iterations.

Permutations

Simply import the permutations module from the itertools python package in your python program. Then you must define a sequence for which you want to find the permutations. Once you defined it, simply pass it as a parameter to the method permutations(). The sequence can be defined using an iterable like a Python List because the method permutations() takes any sequence iterable as an argument.

The return type of this function is an iterable permutation object. You can use a for loop to loop through this object so as to fetch individual permutations contained in the permutations object.

Example. The following python program finds the permutations for a simple sequence ['a', 'b', 'c'] and prints all of them out on the console screen.

#Importing permutations module from itertools python package
from itertools import permutations

#Define Sequence to find its permutations
sequence = ['a','b','c']

#Applying permutations method
permutations = permutations(sequence)

#Permutations Interable Object
print(permutations)

#Loop Through using For Loop
for permutation in permutations:
    print(permutation)

The permutations object so returned using this method contains each permutation in the form of a tuple. You can easily join the elements of the tuple to form a string. We have a similar tutorial already published on our blog which is linked below.

Python Permutations Using Itertools Example

Note. If there are x number of elements in your sequence then x! number of permutations will be produced by this method. The permutations method also can produce permutations of some pre-defined length with the given sequence. For this kind of permutation, you must provide the number of elements to be included in each permutation as the second argument to the permutations() method and it should be a positive integer.

#Importing permutations module from itertools python package
from itertools import permutations

#Define Sequence to find its permutations
sequence = ['a','b','c']

#Applying permutations method with length of each permutation set to be 2
permutations = permutations(sequence, 2)

#Permutations Interable Object
print(permutations)

#Loop Through using For Loop
for permutation in permutations:
    print(permutation)

Python Permutations Of Defined Length Example

If there are x number of elements in the sequence, then xCr * r! permutations will be generated using the above method of permutations. In the above example, the value of x is 3 and the value of r is 2, therefore, 6 permutations are generated. (3C2 = 3 and 2! = 2, therefore 3 * 2 = 6)

Combinations

Combinations are different from permutations. You must always provide the value of r i.e. how many you want to select from the total number of elements in the sequence i.e. x. Therefore, this combination is denoted as xCr.

You might be knowing that you can find combinations with replacement and without replacement. In other words, replacement specifies that you can choose the same element to be selected again in the combinations so formed or without replacement means the same elements should not be chosen to form the combinations more than once. We’ve given examples to illustrate both of the cases as itertools provide different combination methods for both of these.

Example. The following python code finds out the possible combinations of 2 letters among the sequence of three letters without replacement.

#Importing combinations module from itertools python package
from itertools import combinations

#Define Sequence to find its combinations
sequence = ['a','b','c']

#Applying combinations method with the length of each permutation set to be 2
combinations = combinations(sequence, 2)

#combinations Interable Object
print(combinations)

#Loop Through using For Loop
for combination in combinations:
    print(combination)

The sequence contains three different letters and we’re choosing 2 letter combinations from this sequence. Therefore, 3C2 = 3, combinations are possible without replacement.

C(x,r) = C(3,2)
= 3! / (2!(3−2)!)
= 3

Python Combinations Without Replacement Example

Note. Combinations are considered to be unique according to their positions in the provided iterable sequence and not according to the values. So if your provided sequence even contains the same values more than once, they will still be considered unique for combinations. But if all the elements in the provided sequence are unique, all the combinations will be unique.

Example. This illustrates the concept of the above note.

#Importing combinations module from itertools python package
from itertools import combinations

#Define Sequence to find its combinations
sequence = ['a','a','b']

#Applying combinations method with the length of each permutation set to be 2
combinations = combinations(sequence, 2)

#combinations Interable Object
print(combinations)

#Loop Through using For Loop
for combination in combinations:
    print(combination)

Observe the output carefully. We’ve provided 3 letters in the sequence, but the first and the second element of the sequence is the same. So, as thinking humanly, they should form repeated combinations. But the itertools combinations method will consider both of these elements as unique according to their indexes and do not see if the values are the same. Hence the combination ('a', 'b') is repeated twice, although it should come once.

Python Combinations Example With Replacement According To Position

Example. The following python code generates all the possible combinations of 2 letters among the sequence of three letters with replacement.

#Importing combinations module from itertools python package
from itertools import combinations_with_replacement

#Define Sequence to find its combinations
sequence = ['a','a','b']

#Applying combinations method with the length of each permutation set to be 2
combinations = combinations_with_replacement(sequence, 2)

#combinations Interable Object
print(combinations)

#Loop Through using For Loop
for combination in combinations:
    print(combination)

Note. Make sure that you also import combinations_with_replacement module from the itertools as well instead of other simple combinations module.

Python Combinations With Replacement Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *