Python itertools.repeat Function

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The itertools.repeat function in Python's itertools module returns an iterator that repeats a single value infinitely or a specified number of times. It is useful for generating constant sequences or combining with other iterators.

Table of Contents

  1. Introduction
  2. itertools.repeat Function Syntax
  3. Examples
    • Basic Usage
    • Specifying the Number of Repeats
    • Using repeat with map
    • Combining repeat with Other Itertools Functions
  4. Real-World Use Case
  5. Conclusion

Introduction

The itertools.repeat function creates an iterator that repeats a given value indefinitely or for a specified number of times. This can be useful for tasks that require a constant sequence or when you need to pair a single value with each item in another iterable.

itertools.repeat Function Syntax

Here is how you use the itertools.repeat function:

import itertools

iterator = itertools.repeat(object, times=None)

Parameters:

  • object: The value to be repeated.
  • times: Optional. The number of times to repeat the value. If not specified, the value is repeated indefinitely.

Returns:

  • An iterator that repeats the given value.

Examples

Basic Usage

Create an iterator that repeats a value indefinitely.

Example

import itertools

repeat_iterator = itertools.repeat('A')
for _ in range(5):
    print(next(repeat_iterator), end=' ')

Output:

A A A A A 

Specifying the Number of Repeats

Create an iterator that repeats a value a specific number of times.

Example

import itertools

repeat_iterator = itertools.repeat('B', times=3)
for item in repeat_iterator:
    print(item, end=' ')

Output:

B B B 

Using repeat with map

Combine repeat with map to apply a function to a constant value paired with each item in another iterable.

Example

import itertools

numbers = [1, 2, 3]
squares = map(pow, numbers, itertools.repeat(2))
print(list(squares))

Output:

[1, 4, 9]

Combining repeat with Other Itertools Functions

Use repeat with zip to pair a constant value with each item in another iterable.

Example

import itertools

letters = ['a', 'b', 'c']
paired = zip(itertools.repeat(1), letters)
print(list(paired))

Output:

[(1, 'a'), (1, 'b'), (1, 'c')]

Real-World Use Case

Filling a List with a Constant Value

Use repeat to initialize a list with a constant value.

Example

import itertools

constant_value_list = list(itertools.repeat(0, times=10))
print(constant_value_list)

Output:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

Conclusion

The itertools.repeat function is used for creating an iterator that repeats a value either indefinitely or a specified number of times. It is useful for generating constant sequences, initializing lists, and combining with other iterators for various tasks.

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare