URL: https://www.progressiverobot.com/permutation-and-combinatios-in-python/

Permutations and Combinations of a set of elements are different arrangements of the elements of the set.

  • Combination is a collection of the elements where the order doesn't matter
  • Permutation is an arrangement of a set where the order does matter.

Let's consider a set as :

				
					{A, B, C}
				
			

The permutations of the above set are as follows :

				
					('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')
				
			

The combinations of the above set when two elements are taken together are :

				
					('A', 'B')
('A', 'C')
('B', 'C')
				
			

In this tutorial, we will learn how to get the permutations and combinations of a group of elements in Python. We will look at sets of characters and numbers.

We will be using the combinations() and permutations() methods under the [itertools](/community/tutorials/python-itertools) module of Python.

Let's get started.

Permutations of Numeric data

permutations illustration for: Permutations of Numeric data

To use the permutations() method under itertools module we will first need to import the module.

				
					import itertools
				
			

Now let's define a set of numbers.

				
					val = [1, 2, 3, 4]
				
			

Now too get the list of permutations let's use the permutations() method.

				
					perm_set = itertools.permutations(val)
				
			

The line of code above gives an itertools object. To print the different permutations we will iterate over this object.

				
					for i in perm_set:
    print(i)
				
			

We get the output as :

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

The complete code for this section is given below :

				
					import itertools
 
val = [1, 2, 3, 4]
 
perm_set = itertools.permutations(val)
 
for i in perm_set:
    print(i)
				
			

Permutations of a String

Next we will learn how to get the permutations of characters in a string.

We will be using the permutations() method, but this time we will pass a string as an argument.

				
					import itertools
 
s = "ABC"

perm_set = itertools.permutations(s)
for val in perm_set:
    print(val)
				
			

Output :

				
					('A', 'B', 'C')
('A', 'C', 'B')
('B', 'A', 'C')
('B', 'C', 'A')
('C', 'A', 'B')
('C', 'B', 'A')
				
			

Permutations of fixed length

We can find permutations of a set where we only take a specified number of elements in each permutation. This is similar to nPr in the field of mathematics.

The code for finding permutations of fixed length is given below:

				
					import itertools
 
val = [1, 2, 3, 4]
 
perm_set = itertools.permutations(val,2)
 
for i in perm_set:
    print(i)
				
			

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)
				
			

Combinations of Numeric data

Just like the method permutations(), we can use combinations(), also under itertools to get the combinations of a set.

While calling combinations() we need to pass two arguments, the set for finding combinations of and a number that signifies the length of each combination.

				
					import itertools
 
val = [1, 2, 3, 4]
 
com_set = itertools.combinations(val, 2)
 
for i in com_set:
    print(i)
				
			

Output :

				
					(1, 2)
(1, 3)
(1, 4)
(2, 3)
(2, 4)
(3, 4)
				
			

Combinations of a String

We can also get combinations of a string. To get the combinations of a string, use the following piece of code :

				
					import itertools
 
s = "ABC"
 
com_set = itertools.combinations(s, 2)
 
for i in com_set:
    print(i)
				
			

Output :

				
					('A', 'B')
('A', 'C')
('B', 'C')
				
			

Combinations with replacements

There is another method under the itertools module called combinations_with_replacement(). This method takes under consideration the combination of a number with itself as well.

Let's see how it works.

For numeric set

				
					import itertools
 
val = [1, 2, 3, 4]
 
com_set = itertools.combinations_with_replacement(val, 2)
 
for i in com_set:
    print(i)
				
			

Output :

				
					(1, 1)
(1, 2)
(1, 3)
(1, 4)
(2, 2)
(2, 3)
(2, 4)
(3, 3)
(3, 4)
(4, 4)
				
			

You can see the difference in the output above and the output for the operation of a normal combination. Here we have combinations like (1,1) and (2,2) which are not there in regular combinations operation.

For a string

				
					import itertools
 
val = "ABCD"
 
com_set = itertools.combinations_with_replacement(val, 2)
 
for i in com_set:
    print(i)
				
			

Output :

				
					('A', 'A')
('A', 'B')
('A', 'C')
('A', 'D')
('B', 'B')
('B', 'C')
('B', 'D')
('C', 'C')
('C', 'D')
('D', 'D')
				
			

Conclusion

This tutorial was about finding permutations and combinations of a set in python. We used itertools module available in python to find the permutations and combinations.