# Generate all permutations of a List in Python

In this article, we will discuss different ways to get all permutations of elements in a List.

## Method 1: Using itertools module

The itertools module in Python, provides a function permutations(). It accepts an iterable as argument, and returns a successive r-length permutations of elements in the iterable.

To generate all perumtations of a list, pass the list to the permutations() function as argument, and create a list from the returned values. Let’s see an example,

```from itertools import permutations

listOfNumbers = [1, 2, 3]

# Ger all permutations of a list
permutationList = list(permutations(listOfNumbers))

for subList in permutationList:
print(subList)
```

Output:

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

We create a list of tuples, that contains all the permutations of the original list.

## Method 2: Using Custom Function

We can also create a custom function, that accepts a list as argument, and returns a list of lists, containing all the permutations of given list.

```def GetPermutations(listOFElements):
# Create a empty list of lists
listOfLists = []
if len(listOFElements) <= 1:
# If list has 0 or 1 element, then add in list of lists
listOfLists.append(listOFElements)
else:
# If list has more than 1 elements
# Iterate over all elements by index position
for i in range(len(listOFElements)):
# Select element at ith poistion
elem = listOFElements[i]
# Select remaining elements
remainingList = listOFElements[:i] + listOFElements[i+1:]
# get permutations of all remaining elements, and add ith element to it
for perms in GetPermutations(remainingList):
listOfLists.append([elem] + perms)

# return permutations of given list
return listOfLists
```

Logic of this function is as follows,

• Create an empty list of lists to keep the permutations of given list.
• If the given list is empty. Then it will have no permutations, so return the same empty list only.
• If the given list has only one element. Then it will have only one permutation, so return the same list.
• If the list has more than one element, then iterate over all elements by index position. For each element at ith index,
• Select the element at ith index in a variable, and select remaining elements in a list.
• Get permutations of all remaining elements using reccursion, and add ith element to each of it.
• Add these permutation to list of lists created in first step.
• Once the iteration ends, return the list of list created in first step.

To generate all perumtations of a list, pass the list to our custom function GetPermutations() function as argument, and get a list of lists containing all the permutations. Let’s see a complete example,

```def GetPermutations(listOFElements):
# Create a empty list of lists
listOfLists = []
if len(listOFElements) <= 1:
# If list has 0 or 1 element, then add in list of lists
listOfLists.append(listOFElements)
else:
# If list has more than 1 elements
# Iterate over all elements by index position
for i in range(len(listOFElements)):
# Select element at ith poistion
elem = listOFElements[i]
# Select remaining elements
remainingList = listOFElements[:i] + listOFElements[i+1:]
# get permutations of all remaining elements, and add ith element to it
for perms in GetPermutations(remainingList):
listOfLists.append([elem] + perms)

# return permutations of given list
return listOfLists

listOfNumbers = [1, 2, 3]

# Ger all permutations of a list
permutationList = GetPermutations(listOfNumbers)

for subList in permutationList:
print(subList)
```

Output:

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

We create a list of lists, that contains all the permutations of the original list.

## Summary

We learned about two different ways to generate permutations of a list in Python. Thanks.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top