Permutation is an arrangement of objects in a specific order. Order of arrangement of object is very important. The number of permutations on a set of n elements is given by n!. For example, there are 2! = 2*1 = 2 permutations of {1, 2}, namely {1, 2} and {2, 1}, and 3! = 3*2*1 = 6 permutations of {1, 2, 3}, namely {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2} and {3, 2, 1}.

**Method 1 (Backtracking) **

We can use the backtracking based recursive solution discussed here.

**Method 2 **

The idea is to one by one extract all elements, place them at first position and recur for remaining list.

`# Python function to print permutations of a given list ` `def` `permutation(lst): ` ` ` ` ` `# If lst is empty then there are no permutations ` ` ` `if` `len` `(lst) ` `=` `=` `0` `: ` ` ` `return` `[] ` ` ` ` ` `# If there is only one element in lst then, only ` ` ` `# one permuatation is possible ` ` ` `if` `len` `(lst) ` `=` `=` `1` `: ` ` ` `return` `[lst] ` ` ` ` ` `# Find the permutations for lst if there are ` ` ` `# more than 1 characters ` ` ` ` ` `l ` `=` `[] ` `# empty list that will store current permutation ` ` ` ` ` `# Iterate the input(lst) and calculate the permutation ` ` ` `for` `i ` `in` `range` `(` `len` `(lst)): ` ` ` `m ` `=` `lst[i] ` ` ` ` ` `# Extract lst[i] or m from the list. remLst is ` ` ` `# remaining list ` ` ` `remLst ` `=` `lst[:i] ` `+` `lst[i` `+` `1` `:] ` ` ` ` ` `# Generating all permutations where m is first ` ` ` `# element ` ` ` `for` `p ` `in` `permutation(remLst): ` ` ` `l.append([m] ` `+` `p) ` ` ` `return` `l ` ` ` ` ` `# Driver program to test above function ` `data ` `=` `list` `(` `'123'` `) ` `for` `p ` `in` `permutation(data): ` ` ` `print` `p ` |

*chevron_right*

*filter_none*

Output:

['1', '2', '3'] ['1', '3', '2'] ['2', '1', '3'] ['2', '3', '1'] ['3', '1', '2'] ['3', '2', '1']

**Method 3 (Direct Function) **

We can do it by simply using the built-in permutation function in itertools library. It is the shortest technique to find the permutation.

`from` `itertools ` `import` `permutations ` `l ` `=` `list` `(permutations(` `range` `(` `1` `, ` `4` `))) ` `print` `l ` |

*chevron_right*

*filter_none*

Output:

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

This article is contributed by **Arpit Agarwal**. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Generate a random permutation of elements from range [L, R] (Divide and Conquer)
- Minimum cost to generate any permutation of the given string
- 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
- Program to generate all possible valid IP addresses from given string | Set 2
- Python | Ways to find all permutation of a string
- Generate all unique partitions of an integer
- Generate all palindromic numbers less than n
- Generate all cyclic permutations of a number
- Program to generate all possible valid IP addresses from given string
- Generate all rotations of a number
- Generate all binary strings of length n with sub-string "01" appearing exactly twice
- Minimum number of coins that can generate all the values in the given range
- Generate a string whose all K-size substrings can be concatenated to form the given string
- Generate all numbers up to N in Lexicographical Order
- Generate a unique Array of length N with sum of all subarrays divisible by N
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square
- Generate all possible permutations of a Number divisible by N
- Generate all integral points lying inside a rectangle