Python Function Reference

itertools.permutations

Iterate over the permutations of items, all possible orders.

Python 3.13
itertools.permutations(iterable, r=None)

Parameters

iterableIterable
The items to permute.
rint | Noneoptional
The length of a permutation; all items by default.

Return value

An iterator of tuples.

Examples

Permutations of two items

Python 3.13
from itertools import permutations

print(list(permutations('abc', 2)))
[('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'c'), ('c', 'a'), ('c', 'b')]

Number of permutations of four

Python 3.13
from itertools import permutations

print(len(list(permutations(range(4)))))
24

Details

Order matters: (a, b) and (b, a) are different permutations. Items are distinguished by position, not by value, so repeated items give identical tuples. Without r there are n! permutations, and their number grows quickly.

See also