Describing itertools.repeat(elem, n=None)
- The
repeatfunction repeats an element a given number of times - It returns an iterator of repeated elements
- The element is repeated an
nnumber of times - The
repeatfunction is considered an infinite iterator -
This is because the iterator keeps repeating the element an infinite number of times by default
- If
nis specified, then it will become a finite iterator
- If
Illustrating the repeat Function
>>> from itertools import repeat
>>> nums = repeat(2)
>>> for i in range(5): print(next(nums))
2
2
2
2
2
>>> letters = repeat('a', 3)
>>> for i in range(5): print(next(letters))
'a'
'a'
'a'
StopIterationReferences
Previous
Next