On this page
itertools.repeat
Prev: itertools.product | Up: itertools | Next: itertools.starmap
Rel: itertools.accumulate, itertools.batched, itertools.chain, itertools.combinations
Ref: docs.python.org — itertools.repeat
Tags: #public #python3 #stdlib #itertools
Import
import itertools
from itertools import repeat
Signature & Overview
itertools.repeat(object [,times]) -> create an iterator which returns the object
for the specified number of times. If not specified, returns the object endlessly.
Usage
from itertools import repeat
assert list(repeat("x", 3)) == ["x", "x", "x"]
# Supply a constant argument to map()
assert list(map(pow, [2, 3, 4], repeat(2))) == [4, 9, 16]
Gotchas
- Yields the exact same object reference:
list(repeat([], 3))creates three references to the same list—mutating one mutates all three. - Infinite when
timesis omitted: Always pairrepeat(val)withzip(),map(), orislice().
REPL Inspect
from itertools import repeat
help(repeat)
Linked from
Related notes
- tuts/python3/stdlib_module/itertools/product Linked here; links to this page; shared tags: #itertools, #stdlib
- tuts/python3/stdlib_module/itertools/starmap 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