Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

itertools.repeat

Yields the same object repeatedly, optionally for a fixed number of iterations.

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

Syntax

itertools.repeat(object[, times])

Yields the same object repeatedly, optionally for a fixed number of iterations.

A zero count produces no values. Repeating a mutable object reuses one reference.

The iterator yields its original object each time. A count of three yields three references to x, and zero yields none. Omitting times produces an infinite iterator. Mutating one of several repeated references to a list is visible through the others.

import itertools as I
x = []
a, b = I.repeat(x, 2)
a.append(4)
assert b is x and b == [4] and list(I.repeat(x, 0)) == []

Examples

import itertools as I
print(list(I.repeat("x", 3)))
print(list(I.repeat("x", 0)))

Result

['x', 'x', 'x']
[]

The iterator yields its original object each time. A count of three yields three references to x, and zero yields none. Omitting times produces an infinite iterator. Mutating one of several repeated references to a list is visible through the others.

Sources