On this page
itertools.permutations
Prev: itertools.pairwise | Up: itertools | Next: itertools.product
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.permutations
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import permutations
Signature & Overview
itertools.permutations(iterable, r=None)
Return successive r-length permutations of elements in the iterable.
Usage
from itertools import permutations
assert list(permutations("AB")) == [("A", "B"), ("B", "A")]
assert list(permutations([1, 2, 3], 2)) == [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]
Gotchas
- Order matters: Both
('A', 'B')and('B', 'A')are yielded, unlikecombinations(). - Factorial explosion:
permutations(range(n))yields $n!$ tuples ($10! = 3.6\text{M}$)—never materialize large inputs.
REPL Inspect
from itertools import permutations
help(permutations)
Linked from
Related notes
- tuts/python3/stdlib_module/itertools/pairwise Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/product 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/accumulate Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/batched Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/chain Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations Linked here; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/combinations_with_replacement shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/compress shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/count shared tags: #itertools, #stdlib