itertools module - infinite iterators
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)/programming-techniques/functional-programming/iterators/
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.
See also
Join the PythonInformer Newsletter
Sign up using this form to receive an email when new content is added:
Popular tags
2d arrays abstract data type alignment and angle animation arc array arrays bar chart bar style 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 decorator design pattern device space dictionary drawing duck typing efficiency ellipse else encryption enumerate fill filter font font style for loop formula 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 latex len lerp line line plot line style linear gradient linspace list list comprehension logical operator lru_cache magic method mandelbrot mandelbrot set map marker style matplotlib monad mutability named parameter numeric python numpy object open operator optimisation optional parameter or pandas partial application path pattern permutations pie chart pil pillow polygon pong positional parameter print product programming paradigms programming techniques pure function python standard library radial gradient range recipes rectangle recursion reduce regular polygon repeat rgb rotation roundrect scaling scatter plot scipy sector segment sequence setup shape singleton slice slicing sound spirograph sprite square str stream string stroke structural pattern subpath symmetric encryption template tex text text metrics tinkerbell fractal transform translation transparency triangle truthy value tuple turtle unpacking user space vectorisation webserver website while loop zip zip_longest