Permutation and Combination in Python

Permutations & Combinations In Python

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 and 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 permutation and combinations of any sequence.

The python for loop and 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 permutations object. You can use a for loop to loop through this object so as to fetch individual permutation 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.

The permutations object so returned using this method contains each permutation in the form 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 that 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 permutation of some pre-defined length with the given sequence. For this kind of permutations, 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.

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, with 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 illustrates 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.

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

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.

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.

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.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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