Skip to main content

How to Call Calling Functions Repeatedly

Sometimes you need to call a function a specific number of times.

This guide explores various methods for achieving this in Python, from using for loops with range() to more specialized techniques using itertools.repeat(), list comprehensions, map(), and while loops.

Calling a Function N Times with a for Loop

The most common method for calling a function N times is to use a for loop with the range() function, which generates a sequence of numbers:

def print_message(message):
print(message)

number = 1
for _ in range(5):
number = number * 2
print_message('tutorialreference.com')

print(number) # Output: 32

Output:

tutorialreference.com
tutorialreference.com
tutorialreference.com
tutorialreference.com
tutorialreference.com
32
  • The range(5) creates a sequence of integers from 0 to 4 (exclusive).
  • The for loop iterates over this sequence, calling the function during every iteration.
  • The underscore (_) is used as a convention for placeholder variables (variables that are not used within the loop)

Calling a Function N Times with itertools.repeat()

The itertools.repeat() class provides a more specialized way to create an iterator that returns a specific value repeatedly. This can be more efficient in some cases than range():

from itertools import repeat

def print_message(message):
print(message)

number = 1
for _ in repeat(None, 5):
number = number * 2
print_message('tutorialreference.com')

print(number) # Output: 32

Output:

tutorialreference.com
tutorialreference.com
tutorialreference.com
tutorialreference.com
tutorialreference.com
32
  • itertools.repeat(None, 5) creates an iterator that returns None five times.
  • This can be more efficient in some cases, because it doesn't have to create new integer objects, like range() does. However, this difference in performance will likely be negligible in most applications.

Calling a Function N Times with List Comprehensions

List comprehensions provide a compact way to call a function and collect the results in a list:

def my_function(number):
return number * 5

results = [my_function(10) for _ in range(3)]
print(results) # Output: [50, 50, 50]

The list comprehension [my_function(10) for _ in range(3)] iterates three times and the result of the call to my_function(10) is collected in a list on each iteration.

Calling a Function N Times and Storing Results with a for Loop

You can use a for loop with a list to store the results of function calls:

def increment(num):
return num + 100

results = []
for i in range(1, 6):
results.append(increment(i))

print(results) # Output: [101, 102, 103, 104, 105]
  • The for loop iterates through a sequence of integers using range().
  • In each iteration, the function increment is called and the result is appended to the results list.

Calling a Function N Times with map()

The map() function can be used to call a function with items from an iterable (in this case, a list):

def increment(num):
return num + 100
list_of_arguments = [1, 2, 3, 4, 5]
results = list(map(increment, list_of_arguments))
print(results) # Output: [101, 102, 103, 104, 105]
  • The map() function calls the increment function with every number from the list_of_arguments.
  • The list() constructor is used to convert the map object to a list.

Calling a Function N Times with a while Loop

You can also use a while loop to call a function a specified number of times:

def print_message(message):
print(message)

n = 3
while n > 0:
print_message('tutorialreference.com')
n -= 1

Output:

tutorialreference.com
tutorialreference.com
tutorialreference.com
  • The loop continues as long as the variable n is greater than 0.