Python Glossary Русский

all Python 3itertoolsfunction

itertools.repeat

itertools

Repeat a single value endlessly or exactly `times` times.

Syntax

itertools.repeat(object[, times])

With no times argument the stream is infinite — never wrap it in list(); pair it with zip()/map() to feed a constant alongside each element (map(pow, base, repeat(2))). repeat yields the same object every time, not copies, so with a mutable value every item is a reference to one shared thing.

Examples

import itertools
print(list(itertools.repeat('x', 3)))   # → ['x', 'x', 'x']
print(sum(itertools.repeat(5, 4)))   # → 20
print(list(map(pow, [1, 2, 3], itertools.repeat(2))))   # → [1, 4, 9]
print(list(itertools.islice(itertools.repeat(7), 3)))   # → [7, 7, 7]
print(list(itertools.repeat('x', -1)))   # → []

See also

Open in the full glossarydocs.python.orgReport an inaccuracy