Python Reference ยท Standard Library: Data Structures and Functional Tools
itertools combinatoric functions
Reference for itertools.product, permutations, combinations and combinations_with_replacement: ordering rules, counts, repeat and how to avoid huge outputs.
Four functions in itertools generate arrangements of items: product (Cartesian product), permutations (ordered selections), combinations (unordered selections) and combinations_with_replacement. They return lazy iterators of tuples.
The counts grow explosively, so iterate or count them instead of converting everything to a list.
from itertools import combinations print(list(combinations("abc", 2))) # [('a', 'b'), ('a', 'c'), ('b', 'c')]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Syntax
product(*iterables, repeat=1) permutations(iterable, r=None) combinations(iterable, r) combinations_with_replacement(iterable, r)
| Name | Description |
|---|---|
iterables |
One or more input iterables for product. |
repeat |
Use the inputs this many times; product("01", repeat=3) equals product("01", "01", "01"). |
iterable |
The pool of items. It is read fully and stored as a tuple. |
r |
Length of each result. For permutations, None means the full length. |
| Function | Order matters | Items reused | Count for n items, length r |
|---|---|---|---|
product |
yes | yes | n ** r |
permutations |
yes | no | n! / (n - r)! |
combinations |
no | no | n! / (r! (n - r)!) |
combinations_with_replacement |
no | yes | (n + r - 1)! / (r! (n - 1)!) |
Return value: an iterator of tuples. Raises: ValueError if r is negative; a non-iterable argument raises TypeError.
Usage notes
- Output order is defined.
combinationsandpermutationsfollow the input order: if the input is sorted, the tuples come out in lexicographic order. - Positions, not values, are unique.
permutations([1, 1, 2])yields duplicate-looking tuples. Wrap inset()(andsorted()for a stable print) if you need distinct ones. rlarger than the pool gives an empty result, not an error;r = 0gives a single empty tuple.- Beware of size. 20 items taken 10 at a time is 184,756 combinations but 670 billion permutations. Use
math.comb()andmath.perm()to check the size first. - Streaming is the point. Loop and test each tuple, or use
sum(1 for _ in ...)rather thanlist(...)for big spaces. - Duplicates in the input are treated as separate elements in every function.
- Infinite input is not allowed: the pool is consumed first. Use
isliceon an infinite source before passing it in. - Use
productinstead of nested loops for grids, brute-force parameter sweeps and truth tables.