How to generate a permutation?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A permutation is an ordering of items where position matters. Generating permutations is a common task in combinatorics, brute-force search, test generation, and interview-style algorithm problems. The right technique depends on whether you need all permutations, just one random permutation, or a lazy sequence you can iterate without storing everything at once.
Generate all permutations with a library
If you are using Python, the simplest solution is usually itertools.permutations. It produces permutations lazily, which is much better than building a giant list unless you truly need all results in memory.
This prints:
- '
('a', 'b', 'c')' - '
('a', 'c', 'b')' - '
('b', 'a', 'c')' - and so on
The big advantage is that you do not have to reimplement a standard algorithm for a common task.
Backtracking algorithm
If you need to understand or implement the logic yourself, backtracking is the classic method. At each step, choose one unused element, add it to the current path, recurse, then undo the choice.
This method is easy to reason about and adapts well when you need to add extra constraints, such as "only permutations where adjacent elements differ."
Generate one random permutation
Sometimes you do not need all permutations at all. You just need one shuffled ordering. In that case, generate a random permutation instead of enumerating the factorial-sized search space.
That is far cheaper than generating every possible arrangement and choosing one.
Understand the growth rate
The number of permutations of n distinct items is n!, which grows very quickly:
- '
3! = 6' - '
5! = 120' - '
10! = 3,628,800'
This matters because a solution that feels fine for n = 5 becomes impractical much sooner than many people expect. The algorithm is not necessarily bad; the search space itself is large.
Avoid duplicates when input contains repeated values
If the input contains duplicate elements, naive generation can emit the same permutation more than once. For example, [1, 1, 2] has fewer unique permutations than three distinct elements would.
One fix is to sort the input and skip duplicate choices at the same decision level.
This version avoids duplicate outputs while still using the same backtracking idea.
Common Pitfalls
The first pitfall is materializing every permutation when you only need to iterate through them. Prefer lazy generation when possible.
Another issue is forgetting the factorial growth rate. A function that looks correct can still be unusable on slightly larger input sizes.
Duplicate inputs are another trap. Without duplicate-aware logic, your algorithm may generate repeated answers and waste a lot of time.
Finally, do not reimplement a library solution unless you need custom behavior or you are solving the problem for learning purposes.
Summary
- Use a library helper such as
itertools.permutationswhen available. - Use backtracking when you need to understand or customize the generation logic.
- Use
random.shufflewhen you need one random ordering, not every permutation. - Watch out for factorial growth because permutations become expensive very quickly.
- Handle duplicate input values explicitly if you need unique permutations only.
Related reading
- How to generate a power set of a given set?
- How to generate a random permutation in Java
- How to generate all multiplicative partitions of a number if I have a list of primes/exponents?
- How to generate all permutations of a string in PHP?
- How to generate a subdivided icosahedron?
- How to generate all the permutations of a multiset?
- How to Generate Combinations of Elements of a ListT in .NET 4.0
- How to generate maximally unbalanced AVL trees

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.