Python provide direct methods to find permutations and combinations of a sequence. These methods are present in itertools package.

**Permutation**

First import itertools package to implement permutations method in python. This method takes a list as an input and return an object list of tuples that contain all permutation in a list form.

`# A Python program to print all ` `# permutations using library function ` `from` `itertools ` `import` `permutations ` ` ` `# Get all permutations of [1, 2, 3] ` `perm ` `=` `permutations([` `1` `, ` `2` `, ` `3` `]) ` ` ` `# Print the obtained permutations ` `for` `i ` `in` `list` `(perm): ` ` ` `print` `i ` |

*chevron_right*

*filter_none*

Output

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

It generates n! permutations if length of input sequence is n.

If want want to get permutations of length L then implement it in this way.

`# A Python program to print all ` `# permutations of given length ` `from` `itertools ` `import` `permutations ` ` ` `# Get all permutations of length 2 ` `# and length 2 ` `perm ` `=` `permutations([` `1` `, ` `2` `, ` `3` `], ` `2` `) ` ` ` `# Print the obtained permutations ` `for` `i ` `in` `list` `(perm): ` ` ` `print` `i ` |

*chevron_right*

*filter_none*

Output

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

It generate nCr * r! permutations if length of input sequence is n and input parameter is r.

**Combination**

This method takes a list and a input r as a input and return a object list of tuples which contain all possible combination of length r in a list form.

`# A Python program to print all ` `# combinations of given length ` `from` `itertools ` `import` `combinations ` ` ` `# Get all combinations of [1, 2, 3] ` `# and length 2 ` `comb ` `=` `combinations([` `1` `, ` `2` `, ` `3` `], ` `2` `) ` ` ` `# Print the obtained combinations ` `for` `i ` `in` `list` `(comb): ` ` ` `print` `i ` |

*chevron_right*

*filter_none*

Output

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

- Combinations are emitted in lexicographic sort order of input. So, if the input list is sorted, the combination tuples will be produced in sorted order.
`# A Python program to print all combinations`

`# of given length with unsorted input.`

`from`

`itertools`

`import`

`combinations`

`# Get all combinations of [2, 1, 3]`

`# and length 2`

`comb`

`=`

`combinations([`

`2`

`,`

`1`

`,`

`3`

`],`

`2`

`)`

`# Print the obtained combinations`

`for`

`i`

`in`

`list`

`(comb):`

`print`

`i`

*chevron_right**filter_none*Output

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

- Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.
`# A Python program to print all combinations`

`# of given length with duplicates in input`

`from`

`itertools`

`import`

`combinations`

`# Get all combinations of [1, 1, 3]`

`# and length 2`

`comb`

`=`

`combinations([`

`1`

`,`

`1`

`,`

`3`

`],`

`2`

`)`

`# Print the obtained combinations`

`for`

`i`

`in`

`list`

`(comb):`

`print`

`i`

*chevron_right**filter_none*Output

(1, 1) (1, 3) (1, 3)

- If we want to make combination of same element to same element then we use combinations_with_replacement.
`# A Python program to print all combinations`

`# with an element-to-itself combination is`

`# also included`

`from`

`itertools`

`import`

`combinations_with_replacement`

`# Get all combinations of [1, 2, 3] and length 2`

`comb`

`=`

`combinations_with_replacement([`

`1`

`,`

`2`

`,`

`3`

`],`

`2`

`)`

`# Print the obtained combinations`

`for`

`i`

`in`

`list`

`(comb):`

`print`

`i`

*chevron_right**filter_none*Output

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

- Combination and Permutation Practice Questions | Set 1
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of adjacent swaps required to convert a permutation to another permutation by given condition
- Python | Print all string combination from given numbers
- Python | Extract Combination Mapping in two lists
- Python | All possible N combination tuples
- Python - Smallest integer possible from combination of list elements
- Python - All possible items combination dictionary
- Python - Dictionary values combination of size K
- Python - Character Replacement Combination
- Python - All Position Character Combination
- Python - Filter Strings combination of K substrings
- Python - All combination Dictionary List
- Count number of strings (made of R, G and B) using given combination
- Generate a combination of minimum coins that sums to a given value
- Sum of products of all combination taken (1 to n) at a time
- Generate all permutation of a set in Python
- Python | Permutation of a given string using inbuilt function
- Python | Ways to find all permutation of a string
- SymPy | Permutation.is_Identity() in Python

This article is contributed by **Raju Varshney**. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.