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']
- HowTo
- Python How-To's
- Repeat N Times in Python
Created: February-14, 2021 | Updated: July-18, 2021
- Repeat N Times in Python Using the
range() Function - Repeat N Times in Python Using the
itertools.repeat() Method
In this tutorial, we will look into various methods to
repeat the code N times in Python. In many cases, we need to perform a set of actions on each element of an array, like processing text line by line, performing some mathematical operations on each value of an array or sorting an array or list, etc. We need to repeat some portion of the code for all the tasks mentioned above again and again. This tutorial will look into different methods to repeat the specific task N times in Python. Repeat N Times in Python Using the range() FunctionThe most common way to repeat a specific task or operation N times is by using the for loop in programming. We can iterate the code lines N times using the for loop with the range() function in Python. The range(start, stop, step) function returns the sequence of numbers starting from the value specified in the start argument (equal to 0 by default), till the value
specified in the stop argument. The step argument specifies the step size of the sequence returned by the range() function, and its value is set to 1 by default. The below code example demonstrates how to create a for loop with the range() method to repeat the code N times:
num = 10
for x in range(num):
#code
Suppose the variable x is not desired in the code; in that case, we can use the for loop in the following way. _
is used as a throwaway variable in the loop. num = 10
for _ in range(num):
#code
The itertools.repeat(val, num) method is an infinite iterator, which means it will iterate infinitely till the break statement if the num value (which represents the number of iterations) is not provided. The val parameter of this method represents the value that will be printed on each iteration. As we want to repeat the iteration N times, we will pass the value of N to the num
argument and None value to the val argument since we do not need to print anything. The itertools.repeat() method is more efficient than the range() method, but the itertools module needs to be imported to use this method. The below code example demonstrates how to use the itertools.repeat() method to repeat a specific code N times: import itertools
num = 10
for _ in itertools.repeat(None, num):
#code
Related Article - Python LoopAccess the Index in 'Foreach' Loops in PythonSkip Iterations in a Python LoopText Menu With Infinite Loop in PythonLoop Backward Iteration in Python
How do you repeat 3 times in Python?
The Python for statement iterates over the members of a sequence in order, executing the block each time. Contrast the for statement with the ''while'' loop, used when a condition needs to be checked each iteration or to repeat a block of code forever. For example: For loop from 0 to 2, therefore running 3 times.
|