# Generate all permutation of a set in Python

## Generate all permutation of a set in Python

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)
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 permutation 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`

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`

Output:

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

Last Updated on October 28, 2021 by admin

## median() function in Python statistics modulemedian() function in Python statistics module

median() function in Python statistics module Python is a very popular language when it comes

## Replace NaN Values with Zeros in Pandas DataFrameReplace NaN Values with Zeros in Pandas DataFrame

Missing data in a Pandas DataFrame is commonly represented as NaN (Not a Number) values.

## Convert PDF to Image using PythonConvert PDF to Image using Python

Convert PDF to Image using Python Many tools are available on the internet for converting

## Declare an empty List in PythonDeclare an empty List in Python

Declare an empty List in Python Lists are just like the arrays, declared in other

## Python – Remove first element of listPython – Remove first element of list

Python – Remove first element of list Queue data structure is very well known data

## Best Python libraries for Machine LearningBest Python libraries for Machine Learning

Best Python libraries for Machine Learning Machine Learning, as the name suggests, is the science

## Pass by reference vs value in PythonPass by reference vs value in Python

Pass by reference vs value in Python Developers jumping into Python programming from other languages