On this page
itertools.combinations
Prev: itertools.chain | Up: itertools | Next: itertools.combinations_with_replacement
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations_with_replacement
Ref: docs.python.org — itertools.combinations
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import combinations
Signature & Overview
itertools.combinations(iterable, r)
Return successive r-length combinations of elements in the iterable.
Usage
from itertools import combinations
assert list(combinations("ABC", 2)) == [("A", "B"), ("A", "C"), ("B", "C")]
Gotchas
- Order doesn't matter and no repeats by position: Uniqueness is based on element position, not value—
combinations("AA", 2)still yields[('A', 'A')]. - Combinatorial growth: Output size is $\binom{n}{r}$; avoid
list(...)on large $n$.
REPL Inspect
from itertools import combinations
help(combinations)
Linked from
- tuts/python3/stdlib_module/itertools
- tuts/python3/stdlib_module/itertools/accumulate
- tuts/python3/stdlib_module/itertools/batched
- tuts/python3/stdlib_module/itertools/chain
- tuts/python3/stdlib_module/itertools/combinations_with_replacement
- tuts/python3/stdlib_module/itertools/compress
- tuts/python3/stdlib_module/itertools/count
- tuts/python3/stdlib_module/itertools/cycle
- tuts/python3/stdlib_module/itertools/dropwhile
- tuts/python3/stdlib_module/itertools/filterfalse
- tuts/python3/stdlib_module/itertools/groupby
- tuts/python3/stdlib_module/itertools/islice
- tuts/python3/stdlib_module/itertools/pairwise
- tuts/python3/stdlib_module/itertools/permutations
- tuts/python3/stdlib_module/itertools/product
- tuts/python3/stdlib_module/itertools/repeat
- tuts/python3/stdlib_module/itertools/starmap
- tuts/python3/stdlib_module/itertools/takewhile
- tuts/python3/stdlib_module/itertools/tee
- tuts/python3/stdlib_module/itertools/zip_longest
Related notes
- tuts/python3/stdlib_module/itertools/accumulate Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/itertools/compress links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/count links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/cycle links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/dropwhile links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/filterfalse links to this page; shared tags: #itertools, #stdlib