How do I generate all permutations of a list?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Generating all Permutations of a List
Permutations are arrangements or sequences in which a set of items can be ordered. Generating all permutations of a list is a common task in computer science and combinatorics, useful in problem-solving, game theory, probabilistic studies, and more.
Understanding Permutations
For a list of length , there are (n factorial) possible permutations. Factorial is defined as the product of all positive integers up to :
This implies that generating permutations becomes computationally intensive for large lists.
Techniques for Generating Permutations
There are various ways to generate permutations programmatically:
1. Recursive Method
A recursive approach to generating permutations involves selecting an element and recursively generating permutations of the remaining elements. Here's an implementation in Python:
Explanation
- Base Case: If the list is empty, there is a single permutation: the empty list.
- For each index, exclude the element and recursively generate permutations of the remainder.
- Prepend the selected element to each generated permutation and return.
2. Itertools Library
Python's itertools module provides a powerful and efficient way to generate permutations:
Explanation
itertools.permutationsgenerates permutations as tuples lazily, meaning on-demand without storing all at once.- Returns an iterator, which is memory efficient compared to generating all permutations before iterating.
3. Non-Recursive (Iterative) Approach
An iterative method can be devised by simulating a recursive stack with explicit use of data structures like stacks:
Explanation
- Uses a stack to handle the backtracking logic required for permutation generation.
- Iteratively builds permutations, storing partial permutations and the remaining items on the stack.
Performance Consideration
Generating permutations directly depends on list size:
- Small Lists: Simple recursive or
itertoolsmethods are efficient. - Large Lists: Memory and computational limits are quickly reached due to the factorial growth in the number of permutations.
Applications of Permutations
- Algorithm Design: Exploring all possible states or configurations.
- Cryptography: Permutation ciphers.
- Games and Puzzles: Solving puzzles like the Rubik's cube or Sudoku.
Summary Table
| Method | Description | Pros | Cons |
| Recursive | Uses recursion for permutations | Simple, clear logic | High memory usage for large lists (Recursive depth) |
| Itertools (Python) | Utilizes itertools.permutations | Efficient, low memory footprint | Only available in Python |
| Iterative (Non-recursive) | Uses an explicit stack for permutation logic | No recursion overhead | Complex implementation for beginners |
Conclusion
Generating permutations is a fundamental concept with wide-ranging applications. It is essential to choose the right method based on the list size and resource constraints. For Python users, leveraging the itertools library is highly recommended due to its efficiency and simplicity. However, understanding recursive and iterative forms enriches problem-solving skills and offers deep insights into algorithm design.
Related reading
- How do I generate all permutations of a list?
- How do I get the intersection between two arrays as a new array?
- How do I incorporate the Reddit page/post ranking algorithm?
- How do I initialize the t-variables in A Fast Voxel Traversal Algorithm for Ray Tracing?
- How do I get a plist as a Dictionary in Swift?
- How do I get a plist as a Dictionary in Swift?
- How do I generate points that match a histogram?
- How do I perform a pairwise binary operation between the elements of two containers?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.