Why does Python's itertools.permutations contain duplicates? When the original list has duplicates
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
itertools.permutations works on element positions, not element uniqueness. When input values repeat, different positional selections can produce visually identical tuples. That is why duplicates appear in the output even though the algorithm is behaving correctly.
Position-Based Permutation Logic
Consider input [1, 1, 2]. The two 1 values occupy different indices, so swapping them counts as a different positional permutation even if tuple values look the same.
You get 3! = 6 positional permutations, including repeated tuples like (1, 1, 2).
Remove Duplicates with set
If unique value permutations are needed, wrap output with set.
This is simple and often enough for small inputs.
Compute Unique Counts Without Enumerating All
You can compute theoretical unique count using multinomial logic.
For values with multiplicities n1, n2, ... in total length n, unique count is n! / (n1! * n2! * ...).
This avoids generating huge intermediate lists when only counts are required.
Memory-Aware Unique Generation
set(permutations(...)) can consume large memory. For bigger inputs, sort data and use backtracking that skips duplicate branches.
This generates only unique permutations directly.
Practical Use Cases
In combinatorial search, distinct-value permutations are often what you want. In cryptographic or position-sensitive simulations, positional permutations may still be valid even with repeated values. Clarify requirement before choosing dedup strategy.
Also test both correctness and memory footprint because input size grows factorially.
Unique Permutations by Counter-Based Backtracking
For larger inputs, generating only unique permutations is more efficient than generating all and deduplicating.
This technique scales better in memory than set(permutations(...)) for repeated values.
Choosing Representation for Downstream Tasks
If permutations feed another algorithm, think about representation early. Tuples are hashable and work with sets. Lists are mutable and friendlier for in-place edits. Representation choice affects both speed and API compatibility in later pipeline stages.
Benchmarking Dedup Strategies
For small lists, set(permutations(...)) is usually fine. For repeated runs or larger multisets, direct unique generation often wins in memory and runtime. Measure both approaches with representative input sizes before choosing a production implementation.
Common Pitfalls
- Assuming
itertools.permutationsdeduplicates values automatically. - Using
seton very large permutation spaces and exhausting memory. - Confusing positional uniqueness with value uniqueness.
- Computing full permutations when only count is needed.
- Forgetting factorial growth and running expensive brute-force loops.
Summary
itertools.permutationsis index-position based, so duplicates with repeated values are expected.- Use
setfor simple deduplication on small inputs. - Use multinomial formula for unique counts without full generation.
- Use skip-duplicate backtracking for memory-aware unique generation.
- Choose algorithm based on whether you need positional or value-level uniqueness.

