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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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 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.

- Convert a List To String in Python (Converting a tuple to a string is exactly the same as you convert a python list into a string.)

**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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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) |

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*.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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.

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

**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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#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.

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!