Python Concepts/Iteration and Iterators

Objective

 * What is iteration?


 * Why is iteration important?


 * What are examples of iterables?


 * What is the difference between an iterable and an iterator?


 * What are examples of iterators?


 * May an iterator be user-written?

Lesson
Iteration is the process of moving from one member of a sequence to the next. Depending on the code that accesses the sequence, the member currently accessed may be retrieved, changed or ignored.

For iteration to be possible, the sequence or object must be "iterable."

Python's error messages tell us whether or not an object is iterable:

object is iterable. Value 6 does not exist in the first list above. It exists in the second.

Use built-in function  on an iterator:

A  is an iterable, but it is not an iterator.

Iteration is important because the concept is so elementary that it is almost impossible to write meaningful code without it.

Iterables
Many common and familiar sequences are iterables. Python's  built-in function accepts iterable as input:

When we say "dictionary is iterable," iteration over a dictionary means iteration over the keys of the dictionary.

When an object is iterable, we expect it to support operations over iterables:

If you know exactly how many items the iterable contains, the following syntax is possible:

If the iterable contains one member:

Iterators
If interrupted, iteration over an iterable does not resume at the point of interruption. If iteration is interrupted and resumed, the next iteration returns to the beginning.

Iterators within Python allow for interruption of the iteration and resumption at the point immediately after that at which interruption occurred.

Function
To create an iterator, use built-in function  with an iterable as argument.

The built-in function  accepts a default argument. If provided, it will be returned if the iterator is exhausted.

An iterator can behave like an iterable:

Iterator may be initialized at any time.

After initialization, original iterable may be changed without affecting original result.

Examples of iterators:

Derived from generator expression
We are familiar with list comprehensions "listcomps":

and also with set comprehensions. Is a set comprehension a "setcomp" ?

A generator expression appears to have the syntax of a tuple comprehension.

Generator object may be used as iterable.

Generator object may be used as iterator.

As for listcomps, conditions may be added.

Derived from generator
Generators are special functions that simplify the task of writing iterators. Regular functions compute a value and return it, but generators return an iterator that returns a stream of values.

Suppose that the method  does not exist. You want to create an iterator that will iterate over and return all the substrings that match.

s1 = ''' The quick, brown fox jum......

go1 =  # generator object derived from function words_iter.

<_sre.SRE_Match object; span=(0, 9), match='The quick'> <_sre.SRE_Match object; span=(11, 20), match='brown fox'>

The quick brown fox jum

'brown' in go1: True

An endless, but controllable generator
At times it's convenient to have a generator that provides endless iteration but also terminates on command. The code below accomplishes this by communicating with the generator via the generator's  method.

count = 4 count = 5 count = 6 count = 7 v = None

Endless generator in a
Listcomps accept free-format Python. Modify the syntax of the loop and it fits in a listcomp. The following example mimics.

L2 = ['abcd 123   ']

Enhanced constructor
Python's  constructor accepts only integers as input.

Produce an enhanced version of  that is an iterator accepting something like:

Further Reading or Review

 * Previous Lesson: Regular Expressions
 * This Lesson: Iteration and Iterators
 * Next Lesson: Using Python's Decimal module
 * Course Home Page