All Possible Combinations of a list of Values
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people ask for "all possible combinations" of a list, they usually mean one of two things: all subsets of all sizes, or all combinations of a specific size k. That distinction matters because the implementation and the result size both depend on whether order matters and whether repeated values are allowed.
Combinations Versus Permutations
A combination ignores order. For example, ['a', 'b'] and ['b', 'a'] represent the same combination. A permutation treats them as different.
So before writing code, decide which question you actually need to answer:
- choose
kitems without caring about order, - generate every subset of the list,
- or generate every ordered arrangement.
For ordinary combinations in Python, itertools.combinations is the standard tool.
Combinations of a Fixed Size
If you want all combinations of size k, use itertools.combinations(values, k).
Output:
This is efficient and expressive. It also makes the size rule explicit.
All Combination Sizes, Also Called the Power Set
If you want every subset from size 0 up to n, build them by chaining combinations for each size.
Output:
That full set of subsets is often called the power set.
A Generator Version Is Better for Large Inputs
The number of combinations grows very quickly. If you do not need them all at once, yield them lazily instead of building one huge list.
This avoids storing the entire result in memory, which matters because the power set of a list of length n has 2^n subsets.
Duplicates in the Input
If the original list contains duplicate values, combinations operate on positions, not on mathematical set uniqueness.
Output:
If you need unique value combinations, you may need to deduplicate the input first or deduplicate the generated results afterward, depending on the intended semantics.
Know the Size Explosion
Combination generation becomes expensive fast. For a list of length 20, the power set already contains more than one million subsets. That means the real engineering question is sometimes not "how do I generate all combinations?" but "do I actually need all of them materialized?"
For many search problems, a lazy iterator or a pruning strategy is better than generating everything first.
Common Pitfalls
- Mixing up combinations with permutations even though order matters in only one of them.
- Asking for "all combinations" without deciding whether that means a fixed size or every subset size.
- Materializing the entire power set for large inputs and running into memory or performance problems.
- Forgetting that duplicate input values can lead to repeated-looking combinations.
- Writing a custom recursive generator when
itertools.combinationsalready solves the normal case cleanly.
Summary
- A combination ignores order, unlike a permutation.
- Use
itertools.combinations(values, k)for combinations of a fixed size. - Build the power set by generating combinations for every size from
0ton. - Prefer a generator when the number of combinations may become large.
- Be explicit about duplicates and result size before assuming you really want every possible combination.

