Python Itertools.repeat() Function with Examples

Itertools Module:

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.

Itertools.repeat() Function:

Itertools.repeat() belongs to the class of infinite iterators. In repeat(), we provide the data as well as the number of times the data will be repeated. If we don’t specify a number, it will loop indefinitely. The memory space is not created for each variable in repeat(). Rather, it creates only one variable and then repeats it.

Syntax:

repeat(value, number)

Parameters

value: It is the value that will be printed.

number: If the optional keyword number is specified, it prints the passed value number times; otherwise, it prints the passed value infinite times.

Examples:

Example1:

Input:

Given value = 60
Given number = 5

Output:

The list of given value { 60 } 5 times =  [60, 60, 60, 60, 60]

Example2:

Input:

Given string = "hello"
Given number = 3

Output:

The list of given string { hello } 3 times =  ['hello', 'hello', 'hello']

Itertools.repeat() Function with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import itertools module using the import keyword.
  • Give the value as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Pass the given value and number as the arguments to the itertools.repeat() function that gets the given value, given number of times and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the value as static input and store it in a variable.
gvn_valu = 60
# Give the number as static input and store it in another variable.
gvn_numb = 5
# Pass the given value and number as the arguments to the itertools.repeat()
# function that gets the given value, given number of times
# and store it in a variable.
rslt = itertools.repeat(gvn_valu, gvn_numb)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The list of given value {", gvn_valu, "}",
      gvn_numb, "times = ", rslt_lst)

Output:

The list of given value { 60 } 5 times =  [60, 60, 60, 60, 60]
For Strings

Approach:

  • Import itertools module using the import keyword.
  • Give the string as static input and store it in a variable.
  • Give the number as static input and store it in another variable.
  • Pass the given string and number as the arguments to the itertools.repeat() function that gets the given string, given number of times and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the string as static input and store it in a variable.
gvn_str = "hello"
# Give the number as static input and store it in another variable.
gvn_numb = 3
# Pass the given string and number as the arguments to the itertools.repeat()
# function that gets the given string, given number of times
# and store it in a variable.
rslt = itertools.repeat(gvn_str, gvn_numb)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The list of given string {", gvn_str, "}",
      gvn_numb, "times = ", rslt_lst)

Output:

The list of given string { hello } 3 times =  ['hello', 'hello', 'hello']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import itertools module using the import keyword.
  • Give the value as user input using the int(input()) function and store it in a variable.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Pass the given value and number as the arguments to the itertools.repeat() function that gets the given value, given number of times and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the value as user input using the int(input()) function and store it in a variable.
gvn_valu = int(input("Enter some random number = "))
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given value and number as the arguments to the itertools.repeat()
# function that gets the given value, given number of times
# and store it in a variable.
rslt = itertools.repeat(gvn_valu, gvn_numb)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The list of given value {", gvn_valu, "}",
      gvn_numb, "times = ", rslt_lst)

Output:

Enter some random number = 75
Enter some random number = 7
The list of given value { 75 } 7 times = [75, 75, 75, 75, 75, 75, 75]
For Strings

Approach:

  • Import itertools module using the import keyword.
  • Give the string as user input using the input() function and store it in a variable.
  • Give the number as user input using the int(input()) function and store it in another variable.
  • Pass the given string and number as the arguments to the itertools.repeat() function that gets the given string, given number of times and store it in a variable.
  • Convert the above result into a list using the list() function and store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import itertools module using the import keyword.
import itertools
# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Give the number as user input using the int(input()) function and store it in another variable.
gvn_numb = int(input("Enter some random number = "))
# Pass the given string and number as the arguments to the itertools.repeat()
# function that gets the given string, given number of times
# and store it in a variable.
rslt = itertools.repeat(gvn_str, gvn_numb)
# Convert the above result into a list using the list() function and store it in
# another variable.
rslt_lst = list(rslt)
# Print the above result.
print("The list of given string {", gvn_str, "}",
      gvn_numb, "times = ", rslt_lst)

Output:

Enter some random string = btechgeeks
Enter some random number = 2
The list of given string { btechgeeks } 2 times = ['btechgeeks', 'btechgeeks']