# itertools module - infinite iterators

Martin McBride, 2021-10-11
Tags count cycle repeat itertools python standard library
Categories python standard library

The itertools module provides several infinite iterators. These will return a potentially infinite number of values.

The iterators in this section are:

• count
• cycle
• repeat

## count

The count function returns an iterator that creates values 0, 1, 2 ... and keeps going forever. It works a bit like the range function, except that it has no upper limit. Here is an example:

for i in itertools.count():
print(i)


This loop will print 0, 1, 2 and so on. Since the count function will carry on forever, you will normally need some kind of break statement in the loop to force it to stop at some point.

The count function has two optional parameters, start which sets the start value (default 0), and step which sets the step value (default 1).

One interesting use of count is with the zip function, to combine two or more iterators. Here is an example:

k = ['a', 'b', 'c']

for i, c in zip(itertools.count(2, 3), k):
print(i, c)


This combines a count that starts from 2 in steps of 3, and the list k. Since zip terminates when the shortest sequence ends, the loop will only run 3 times (because k has length 3). Here is what it prints:

2 a
5 b
8 c


This is a bit like the enumerate function, but with the option to have a step value.

## cycle

The cycle function creates an infinite iterator based on an iterable. When the iterable is used up, cycle will repeat the values, indefinitely. For example:

k = [2, 4, 6, 8]

for i in itertools.cycle(k):
print(i)


This will print 2, 4, 6, 8, 2, 4, 6, 8 ... forever.

## repeat

repeat is similar to cycle, but it only has one value that it repeats over and over. It will continue indefinitely unless the optional times parameter is supplied:

for i in itertools.repeat(5, times=3):
print(i)


This prints 5, 5, 5 because the times parameter is set to 3. If you omit this parameter, it will repeat forever.

Here is a diagram of count, cycle and repeat:

### Potential problem with infinite iterators

The following code can cause problems:

k = list(itertools.count())


The problem here is that list wants to create a list containing every element in the iterator returned by itertools.count(). To do this it will attempt to loop through every element in the iterator and store it in memory. But since the list is infinite, this process will never complete successfully.

list will go into a tight, infinite loop, allocating itself huge amounts of memory.

Depending on how well your operating system handles this, you will probably encounter some degree of instability or slow down. Worst case the UI might become so unresponsive that you are unable to access the system tools to kill the program.

This same caveat applies to any functions that attempt to consume the whole iterable before returning. This includes certain built-in functions (such as sum) that need to consume all the data before returning an answer, and also the combinatoric itertools functions described below.

If you found this article useful, you might be interested in the book Python Quick Start or other books by the same author.

#### Popular tags

2d arrays abstract data type alignment and angle animation arange arc array arrays behavioural pattern bezier curve built-in function callable object chain circle classes clipping close closure cmyk colour combinations comparison operator comprehension context context manager conversion count creational pattern data science data types design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop function function composition function plot functools game development generativepy tutorial generator geometry gif global variable gradient greyscale higher order function hsl html image image processing imagesurface immutable object in operator index inner function input installing iter iterable iterator itertools join l system lambda function len line linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations polygon positional parameter print pure function python standard library radial gradient range recipes rectangle recursion reduce repeat rgb rotation roundrect scaling scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip