combinatorics
permutations
algorithm
list processing
computational methods

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 k items 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).

python
1from itertools import combinations
2
3values = ["a", "b", "c", "d"]
4result = list(combinations(values, 2))
5
6print(result)

Output:

text
[('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd'), ('c', 'd')]

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.

python
1from itertools import combinations
2
3
4def all_combinations(values):
5    result = []
6    for size in range(len(values) + 1):
7        result.extend(combinations(values, size))
8    return result
9
10
11values = ["a", "b", "c"]
12print(all_combinations(values))

Output:

text
[(), ('a',), ('b',), ('c',), ('a', 'b'), ('a', 'c'), ('b', 'c'), ('a', 'b', 'c')]

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.

python
1from itertools import combinations
2
3
4def iter_all_combinations(values):
5    for size in range(len(values) + 1):
6        yield from combinations(values, size)
7
8
9for combo in iter_all_combinations([1, 2, 3]):
10    print(combo)

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.

python
1from itertools import combinations
2
3values = ["a", "a", "b"]
4print(list(combinations(values, 2)))

Output:

text
[('a', 'a'), ('a', 'b'), ('a', 'b')]

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.combinations already 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 0 to n.
  • 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.

Course illustration
Course illustration

All Rights Reserved.