Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Permutations and Combinations using Python

Permutations Combinations Featured Image

In this article, we will be learning how to find permutations and combinations using Python.

Python provides a library named itertools that contains in-built functions to calculate permutations and combinations. Let us quickly look at the implementation of these functions.

Importing the required library

Before we are able to use any of the following functions, we need to import the itertools library. It is done by:

import itertools

The above statement imports the library and forms a pathway to use its functions.


Finding Permutations

Permutation mathematically refers to “the arrangement of certain numbers or letters”. The permutations() function in the itertools library does exactly that.

1. Permutations of a Python string

If we are given a Python string and asked to find out all the ways its letters can be arranged, then the task can easily be achieved by the permutations() function.

import itertools

st = "ABC"

per = itertools.permutations(st)

for val in per:
	print(*val)

Output:

A B C
A C B
B A C
B C A
C A B
C B A

The function permutations() takes a String argument, and provides an itertools object. In case we try to print the variable 'per' directly, we will get the following:

<itertools.permutations object at 0x7fc9abdd8308>

Therefore it is a necessity to run a loop to print each entry.


2. Permutations of multiple numbers

The permuatations() function takes an iterable argument, therefore in order to find out permutations of numbers, we need to pass the numbers as a list, set, or tuple.

import itertools

values = [1, 2, 3]

per = itertools.permutations(values)

for val in per:
	print(*val)

Output:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

In the above techniques to calculate permutations, we are including all the numbers or letters. There is a possibility to restrict the number of elements in the permutations.


3. Permutations with certain number of elements

Similar, to the concept of ‘nPr’, which states “Arranging r elements out of n”, this can be achieved by passing an integer after the set of elements.

import itertools

values = [1, 2, 3, 4]

per = itertools.permutations(values, 2)

for val in per:
	print(*val)

Output:

1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3

In the above code snippet, the permutations() function is asked to arrange only 2 elements at a time from the list of numbers provided.


Finding Combinations

The term Combinations, refer to the ways of picking up elements from a set of objects. The itertools library provides a method combinations() for exactly this functionality.

One thing to note here is that, picking a set of objects does not involve arranging. The combinations() function takes two arguments:

  1. the set of values
  2. an integer denoting the number of values to be picked for a combination.

1. Combinations for letters in a word

Given a word, if we need to find all combinations containing exactly 2 letters from the word, then combinations() is the go-to function.

import itertools

st = "ABCDE"

com = itertools.combinations(st, 2)

for val in com:
	print(*val)

Output:

A B
A C
A D
A E
B C
B D
B E
C D
C E
D E

2. Combinations of set of numbers

Similar to the combinations result we got for letters in a word, it can be achieved for numbers in a list.

import itertools

values = [1, 2, 3, 4]

com = itertools.combinations(values, 2)

for val in com:
	print(*val)

Output:

1 2
1 3
1 4
2 3
2 4
3 4

Note: The combinations generated are based on the index values of each object, not their actual values, therefore in case any value is repeated, then the function prints similar combinations thinking each value is different.


3. Combinations for repeated numbers

To further explain the above Note, let us run an example for it.

import itertools

values = [1, 1, 2, 2]

com = itertools.combinations(values, 2)

for val in com:
	print(*val)

Output:

1 1
1 2
1 2
1 2
1 2
2 2

The result is self-explanatory, as the numbers in the list are repeated.


4. Combinations of numbers with itself

There is yet another function related to permutations and combinations in the itertools library called combinations_with_replacement(). This function is a variation of combinations() function, with a slight difference that it includes combinations of elements with themselves.

import itertools

values = [1, 2, 3, 4]

com = itertools.combinations_with_replacement(values, 2)

for val in com:
	print(*val)

Output:

1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4

We can see the clear distinction of the above output from the output with the same parameters when passed to combinations() function.

There are combinations of numbers with themselves, like there are multiple instances of them in the list. This basically happens because, when we pick an element from the list, the above function, places the same value again, in order to get combinations with itself.

This sums the topic of permutations and combinations using Python.

Conclusion

The application of permutations and combinations are vast in the field of mathematics. The methods explained in this tutorial come handy while solving problems regarding such mathematical techniques.

We hope this article for easy to follow. Feel free to comment below for queries and suggestions.