This article covers iterators, for
loops, and comprehensions.
Iterators
Collections have a mechanism for accessing each item in sequence.
This mechanism is called an iterator
.
Collections which support iteration are said to be iterable
.
Iterable collections have an __iter__
method which returns an iterator
object.
Iterator objects are defined by the methods __iter__
and __next__
.
An iterator’s __iter__
method must return itself,
and its __next__
method must either return the next item in the sequence
or raise a StopIteration
exception.
The iterator for a collection should be obtained using the built-in function iter
.
If the argument to iter
defines __iter__
, then it is called;
otherwise, iter
will return a default iterator.
The default iterator assumes the collection’s __getitem__
method accepts
contiguous, nonnegative int
keys starting at 0
and raises IndexError
when
the key is out of bounds.
Some iterable collections also support reverse iteration, defined by the
__reversed__
method and reversed
built-in function.
There is also a built-in next
function, which calls __next__
on its argument.
Documentation for iter, reversed, and next.
Built-In Functions That Accept Iterables
- sorted: Return a new sorted list from the items in an iterable. For sorting examples and a brief sorting tutorial, see Sorting HOW TO.
- sum: Sums the items in a iterable and returns the result.
- max: Return the largest item in an iterable or the largest of two or more arguments.
- min: Return the smallest item in an iterable or the smallest of two or more arguments.
- all:
Return
True
if all elements of the iterable are true (or if the iterable is empty). - any:
Return
True
if any element of the iterable is true. If the iterable is empty, returnFalse
.
For Loops
Python’s for
loop only works for iterables.
sequence
is evaluated, and the interpreter retrieves an iterator
from the
result.
The iterator’s __next__
method is called, and the return value is stored in
item
.
The loop terminates when the __next__
method raises StopIteration
.
Python will apply sequence unpacking on item
, allowing you to extract the
items in nested sequences as long as you know the size of the inner sequences:
If you want to loop over a sequence of integers (like C++ or Java for
loops),
use the range
type:
Like while
loops, for
loops can have an else
clause.
The else
clause is only executed if the loop terminates by reaching the end of
the sequence (ie it does not execute if the loop terminates due to a break
statement).
To iterate over the indices of a sequence, use range
with len
:
You can get the index and corresponding item using the enumerate function:
You can iterate over parallel sequences using zip:
From the docs on zip:
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using
zip(*[iter(s)]*n)
. This repeats the same iterator n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.
Note that zip
truncates on the shortest sequence, which is why 9
didn’t
appear in the output above.
To work around this, use
itertools.zip_longest.
There are other useful functions in the itertools module.
List, Set, and Dictionary Comprehensions
List Comprehensions offer concise syntax to create lists. Comprehensions are often faster than the equivalent loops, because they can be optimized by the interpreter.
List Comprehensions can contain multiple for
and if
clauses.
Note how the order of the for
and if
statements is the same in both cases.
Python also supports set
comprehensions.
Python also supports dict
comprehensions.