Skip to content

Latest commit

 

History

History
70 lines (66 loc) · 2.33 KB

File metadata and controls

70 lines (66 loc) · 2.33 KB
layout lesson
root .

The itertools module implements a number of iterator building blocks that provide a set of fast, memory efficient tools. These building blocks can be used to form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.

Iterator-based code may be preferred over code which uses lists for several reasons. Since data is not produced from the iterator until it is needed, all of the data is not stored in memory at the same time. Reducing memory usage can reduce swapping and other side-effects of large data sets, increasing performance.

What is an iterator?

An iterator is an object that provides two methods:

  • __next__ which returns the next value from the iterator
  • __iter__ which returns the iterator itself

An iterator behaves like a list of values, with some important differences:

  • The values are generated on demand, so the entire sequence does not need to be stored in memory
  • The values can only be accessed sequentually, so an iterator cannot be accessed like an array
  • The values can only be accessed once
it = iter('PYTHON')

print(it.__next__())

for n, char in enumerate(it):
	print(n, char)

# Iterator completed at this point
# so the following does nothing

for char in enumerate(it):
	print(n, char)
	
it.__next__()

{: .python}

Running this program produces the following output:

P
1 Y
2 T
3 H
4 O
5 N
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

{: .output}

The first 'P' is produced by the call to it.__next__() which returns the first value of the iterator. The next values are produced by the for loop. The second for loop does nothing because at this point the iterator has completed. Finally the program generates a StopIteration exception because an attempt is made to access a completed iterator (the for loop handles this exception rather than passing it to the program.) {: .callout}

Prerequisites

The examples in this lesson can be run directly using the Python interpreter, using IPython interactively, or using Jupyter notebooks. {: .prereq}

Thanks to Mike Driscoll for the content