Python’s Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra.
Note: For more information, refer to Python Itertools
repeat()
itertools.repeat()falls under the category of infinite iterators. In repeat() we give the data and give the number, how many times the data will be repeated. If we will not specify the number, it will repeat infinite times. In repeat(), the memory space is not created for every variable. Rather it creates only one variable and repeats the same variable.
Syntax: repeat(val, num)
Parameters:
val: The value to be printed.
num: If the optional keyword num is mentioned, then it repeatedly prints the passed value num number of times, otherwise prints the passed value infinite number of times.
Example 1:
import itertools
print ("Printing the numbers repeatedly : ")
print (list(itertools.repeat(25, 4)))
|
Output:
Printing the numbers repeatedly :
[25, 25, 25, 25]
Example 2:
import itertools
print(list(map(str.upper,
itertools.repeat('geeksforgeeks', 3))))
|
Output:
['GEEKSFORGEEKS', 'GEEKSFORGEEKS', 'GEEKSFORGEEKS']
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
19 Feb, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...