Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

itertools.permutations

Enumerates ordered selections of distinct input positions.

Version: Python 3.14; repository image, production runtime not attested

Syntax

itertools.permutations(iterable, r=None)

Enumerates ordered selections of distinct input positions.

Positions cannot be reused. Requesting more positions than available yields no tuples.

Order matters: from ABC choose one of three positions then one of two others, producing six pairs, including both AB and BA. Omitting r selects the full input length. Positions cannot be repeated, but equal values from different positions can repeat. Choosing two positions from one yields nothing.

import itertools as I
assert list(I.permutations("AA")) == [("A","A"), ("A","A")]
assert len(list(I.permutations("ABC", 2))) == 6

Examples

import itertools as I
print(list(I.permutations("ABC", 2)))
print(list(I.permutations("A", 2)))

Result

[('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')]
[]

Order matters: from ABC choose one of three positions then one of two others, producing six pairs, including both AB and BA. Omitting r selects the full input length. Positions cannot be repeated, but equal values from different positions can repeat. Choosing two positions from one yields nothing.

Sources