0
0
PythonProgramBeginner · 2 min read

Python Program to Find All Permutations of a List

Use Python's built-in itertools.permutations() function to find all permutations of a list like this: from itertools import permutations; list(permutations(your_list)).
📋

Examples

Input[1]
Output[(1,)]
Input[1, 2]
Output[(1, 2), (2, 1)]
Input[1, 2, 3]
Output[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
🧠

How to Think About It

To find all permutations of a list, think of arranging all elements in every possible order without missing any. We can use a tool that automatically generates these arrangements by picking each element in turn and combining them in all possible ways.
📐

Algorithm

1
Get the input list.
2
Use a function to generate all possible orderings of the list elements.
3
Collect these orderings into a list or another container.
4
Return or print the list of all permutations.
💻

Code

python
from itertools import permutations

items = [1, 2, 3]
all_perms = list(permutations(items))
print(all_perms)
Output
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
🔍

Dry Run

Let's trace the list [1, 2, 3] through the code to see how permutations are generated.

1

Input list

items = [1, 2, 3]

2

Generate permutations

permutations(items) creates an iterator over all orderings of [1, 2, 3]

3

Convert to list

list(permutations(items)) collects all permutations into a list

4

Print result

Prints all 6 permutations as tuples

Permutation
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)
💡

Why This Works

Step 1: Using itertools.permutations

The permutations() function from itertools generates all possible orderings of the input list elements.

Step 2: Collecting permutations

We convert the iterator returned by permutations() into a list to see all permutations at once.

Step 3: Output format

Each permutation is a tuple showing one unique arrangement of the original list elements.

🔄

Alternative Approaches

Recursive approach
python
def permute(lst):
    if len(lst) == 0:
        return [[]]
    result = []
    for i in range(len(lst)):
        rest = lst[:i] + lst[i+1:]
        for p in permute(rest):
            result.append([lst[i]] + p)
    return result

print(permute([1, 2, 3]))
This method uses recursion to build permutations manually but is less efficient and more complex than itertools.
Using backtracking
python
def backtrack(start, lst, result):
    if start == len(lst):
        result.append(lst[:])
        return
    for i in range(start, len(lst)):
        lst[start], lst[i] = lst[i], lst[start]
        backtrack(start + 1, lst, result)
        lst[start], lst[i] = lst[i], lst[start]

result = []
backtrack(0, [1, 2, 3], result)
print(result)
Backtracking swaps elements in place to generate permutations, useful for learning but more complex.

Complexity: O(n!) time, O(n!) space

Time Complexity

Generating all permutations requires factorial time because there are n! possible orderings for a list of length n.

Space Complexity

Storing all permutations also takes factorial space since each permutation is stored in the output list.

Which Approach is Fastest?

Using itertools.permutations() is fastest and simplest; recursive or backtracking methods are educational but slower and more complex.

ApproachTimeSpaceBest For
itertools.permutationsO(n!)O(n!)Fast, simple, built-in
Recursive approachO(n!)O(n!)Learning recursion, manual control
BacktrackingO(n!)O(n!)Understanding in-place swaps
💡
Use itertools.permutations() for a simple and fast way to get all permutations.
⚠️
Trying to generate permutations manually without handling duplicates or missing some arrangements.