itertools.repeat(object, times=None) returns an iterator that yields the same object again and again. If you pass times, it stops after that many items. If you leave times out, it never stops. It is useful when an API expects an iterable and you want a constant value, for example as the second argument to map() or zip().
The object is not copied. Every item the iterator produces is a reference to the same object, which matters for mutable values.
map()zip()repeat() compared with similar tools| Argument | Meaning |
|---|---|
object | The value to produce on every step |
times | Optional number of repetitions; omitted means infinite |
Never call list(repeat(x)) without times. It tries to build an infinite list and runs until memory is exhausted. A negative times is treated as zero, so the iterator is empty.
map()This is the classic use. map() stops at the shortest input, so an infinite repeat() is safe next to a finite iterable.
Here pow(1, 2), pow(2, 2) and so on are computed without allocating a list of twos. For simple cases, a list comprehension such as [n ** 2 for n in numbers] is often clearer, but repeat() helps when you must pass an existing function.
zip()zip() also stops at the shortest iterable, so repeat() can attach a constant to each item.
For the dictionary case, dict.fromkeys(keys, None) does the same job and is easier to read. The repeat() form is more useful when you combine it with other iterator tools.
When you only want to run a loop body N times and do not need the index, for _ in repeat(None, n) avoids creating integer objects for each step. This is the pattern used in the standard library's timeit module.
The gain over for _ in range(n) is tiny in real programs and usually not worth worrying about. Choose range() for readability unless profiling shows a loop overhead problem.
Because repeat() yields the same object every time, changing one item changes them all.
All three entries are the same list. If each item must be independent, build them separately:
The same applies to the expression [[]] * 3, which is a frequent source of bugs for the same reason.
repeat() compared with similar tools| Tool | What it repeats | Use it when |
|---|---|---|
itertools.repeat(x, n) | One value, lazily | You need an iterator of a constant |
[x] * n | One value, as a list | You need a real list of immutable values |
"ab" * 3 | A string or sequence | You are repeating text or a sequence |
itertools.cycle(seq) | A whole sequence, endlessly | You need to loop over a pattern |
itertools.chain.from_iterable(repeat(seq, n)) | A sequence n times, flattened | You need seq repeated n times as a stream |
itertools.repeat() infinite?Only when you omit times. With times=n, it produces exactly n items and then stops.
repeat() copy the object?No. It returns the same reference each time, so mutable objects are shared across all items.
repeat() different from cycle()?repeat() yields one value over and over. cycle() takes an iterable and loops through all of its elements repeatedly.
An iterator created with repeat(x, n) supports len()-like hints through operator.length_hint(), which returns the remaining count. Its repr also shows the remaining times, for example repeat('A', 3).
itertools.repeat(object, times=None) returns a lazy iterator that produces the same object, either times times or forever. Use it to pass constants into map() and zip(), to run a fixed-count loop without an index, and remember that mutable objects are shared rather than copied.