For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
repeat
repeat[ElementType: Copyable & ImplicitlyDestructible](element: ElementType, *, times: Int) -> _RepeatIterator[ElementType]
Constructs an iterator that repeats the given element a specified number of times.
This function creates an iterator that returns the same element over and over for the specified number of times.
Examples:
from std.itertools import repeat
# Repeat a value 3 times
var it = repeat(42, times=3)
for val in it:
print(val) # Prints: 42, 42, 42
# Repeat a string 5 times
var str_it = repeat("hello", times=5)
for s in str_it:
print(s) # Prints: hello, hello, hello, hello, hello
Parameters:
- ElementType (
Copyable&ImplicitlyDestructible): The type of the element to repeat.
Args:
- element (
ElementType): The element to repeat. - times (
Int): The number of times to repeat the element.
Returns:
_RepeatIterator[ElementType]: An iterator that repeats the element the specified number of times.