 # 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

## Python – Split a sentence into list of wordsPython – Split a sentence into list of words

Python | Split a sentence into list of words Given a Sentence, write a Python program to convert the given sentence into list of words. Examples: Input : ['Hello World'] Output : ['Hello', 'world'] Input : ['Geeks For geeks'] Output

## How to split a string in C/C++, Python and Java?How to split a string in C/C++, Python and Java?

How to split a string in C/C++, Python and Java? Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an

## Python – Select random value from a listPython – Select random value from a list

Python | Select random value from a list Generating random numbers has always been an useful utility in day-day programming for games or various types of gambling etc. Hence knowledge and shorthands of it in any programming language is always

## Apply uppercase to a column in Pandas dataframeApply uppercase to a column in Pandas dataframe

Apply uppercase to a column in Pandas dataframe   Analyzing a real world data is some what difficult because we need to take various things into consideration. Apart from getting the useful data from large datasets, keeping data in required

## Difference between == and is operator in PythonDifference between == and is operator in Python

Difference between == and is operator in Python The Equality operator (==) compares the values of both the operands and checks for value equality. Whereas the ‘is’ operator checks whether both the operands refer to the same object or not (present in the same

## Python sorted() functionPython sorted() function

Sorted() function in Python Python sorted() function returns a sorted list from the iterable object. Sorted() sorts any sequence (list, tuple) and always returns a list with the elements in a sorted manner, without modifying the original sequence. Syntax: sorted(iterable, key, reverse)

## Creating Pandas dataframe using list of listsCreating Pandas dataframe using list of lists

Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object. Pandas DataFrame can be created in multiple ways. Let’s discuss how to create Pandas dataframe using list

## Pandas dataframe.aggregate()Pandas dataframe.aggregate()

Pandas dataframe.aggregate() Dataframe.aggregate() function is used to apply some aggregation across one or more column. Aggregate using callable, string, dict, or list of string/callables. Most frequently used aggregations are: sum: Return the sum of the values for the requested axis