# Implementing Iteration¶

## Agenda¶

1. Review: Iteration
2. Details: iterables, iterators, iter, and next
3. Implementing iterators with classes
4. Implementing iterators with generators and yield

## 1. Review: Iteration¶

Iteration simply refers to the process of accessing — one by one — the items stored in some container. The order of the items, and whether or not the iteration is comprehensive, depends on the container.

In Python, we typically perform iteration using the for loop.

In :
# e.g., iterating over a list
l = [2**x for x in range(10)]
for n in l:
print(n)

1
2
4
8
16
32
64
128
256
512

In :
# e.g., iterating over the key-value pairs in a dictionary
d = {x:2**x for x in range(10)}
for k,v in d.items():
print(k, '=>', v)

0 => 1
1 => 2
2 => 4
3 => 8
4 => 16
5 => 32
6 => 64
7 => 128
8 => 256
9 => 512


## 2. Details: iterables, iterators, iter, and next¶

We can iterate over anything that is iterable. Intuitively, if something can be used as the source of items in a for loop, it is iterable.

But how does a for loop really work? (Review time!)

In :
l = [2**x for x in range(10)]

In :
for x in l:
print(x)

1
2
4
8
16
32
64
128
256
512

In :
it = l.__iter__()

In :
type(it)

Out:
list_iterator

An iterable's __iter__ method returns an iterator. This is a separate object (from the container itself) that can be used to iterate over the container's contents.

In :
it.__next__() # re-evaluate this cell with ctrl-enter

Out:
1
In :
l.__next__() # note: lists are not iterators!

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-8-01f476f2762c> in <module>()
----> 1 l.__next__() # note: lists are not iterators!

AttributeError: 'list' object has no attribute '__next__'

An iterator's __next__ method returns the "next" item from the associated container. It raises a StopIteration exception when there are no more items.

In :
# putting it all together
it = l.__iter__()
while True:
try:
print(it.__next__())
except StopIteration:
break

1
2
4
8
16
32
64
128
256
512


Instead of calling __iter__ and __next__ directly, we would typically use the global iter and next functions.

In :
it = iter(l)
while True:
try:
print(next(it))
except StopIteration:
break

1
2
4
8
16
32
64
128
256
512


Of course, we typically would just write:

In :
for x in l:
print(x)

1
2
4
8
16
32
64
128
256
512


Note we can use multiple iterators concurrently — each one keeps track of its own progress through the underlying container.

In :
it1 = iter(l)
it2 = iter(l)
it3 = iter(l)
next(it2)
next(it3)
next(it3)
while True:
try:
print(next(it1), next(it2), next(it3))
except StopIteration:
break

1 2 4
2 4 8
4 8 16
8 16 32
16 32 64
32 64 128
64 128 256
128 256 512


An iterators behavior is not always well defined if the container is modified during the process of iteration:

In :
d = {'a': 'apples', 'b': 'bananas', 'c': 'cats'}
it = iter(d)
print(next(it))
d['d'] = 'dogs'
print(next(it))

a

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
3 print(next(it))
4 d['d'] = 'dogs'
----> 5 print(next(it))

RuntimeError: dictionary changed size during iteration
In :
d = {'a': 'apples', 'b': 'bananas', 'c': 'cats'}
for k in d:
print(k)
d['d'] = 'dogs'

a

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
1 d = {'a': 'apples', 'b': 'bananas', 'c': 'cats'}
----> 2 for k in d:
3     print(k)
4     d['d'] = 'dogs'

RuntimeError: dictionary changed size during iteration

Another detail: iterators are themselves also iterables.

In :
l = [2**x for x in range(10)]
it = iter(l)
for x in it:
print(x)

1
2
4
8
16
32
64
128
256
512


... which means that iterator objects must also implement the __iter__ method (in addition to __next__)

## 3. Implementing iterators with classes¶

In :
class MyIterator:
def __init__(self, max):
self.max = max
self.curr = 0

# the following methods are required for iterator objects

def __next__(self):
if self.curr < self.max:
self.curr += 1
return self.curr
else:
raise StopIteration

def __iter__(self):
return self

In :
it = MyIterator(10)

In :
next(it)

Out:
5
In :
it = MyIterator(10)
while True:
try:
print(next(it))
except StopIteration:
break

1
2
3
4
5
6
7
8
9
10

In :
it = MyIterator(10)
for i in it:
print(i)

1
2
3
4
5
6
7
8
9
10


For a container type, we need to implement an __iter__ method that returns an iterator.

In :
class ArrayList:
def __init__(self):
self.data = []

def append(self, val):
self.data.append(None)
self.data[len(self.data)-1] = val

def __iter__(self):
class ArrayListIterator:
def __init__(self, lst):
self.idx = 0
self.lst = lst

def __iter__(self):
return self

def __next__(self):
if self.idx < len(self.lst.data):
self.idx += 1
return self.lst.data[self.idx-1]
else:
raise StopIteration()
return ArrayListIterator(self)

In :
l = ArrayList()
for x in range(10):
l.append(2**x)

In :
it = iter(l)

In :
type(it)

Out:
__main__.ArrayList.__iter__.<locals>.ArrayListIterator
In :
next(it)

Out:
32
In :
for x in l:
print(x)

1
2
4
8
16
32
64
128
256
512


## 4. Implementing iterators with generators and yield¶

What's a "generator"?

In :
g = (2**x for x in range(10)) # NOT a list comprehension!

In :
type(g)

Out:
generator
In :
len(g)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-baf0f4fee012> in <module>()
----> 1 len(g)

TypeError: object of type 'generator' has no len()
In :
# try to re-evaluate this cell
for x in g:
print(x)

1
2
4
8
16
32
64
128
256
512


A generator object is another type of iterator. We can use list comprehension like syntax to create a generator, but what we get is NOT a list!

The generator object doesn't actually get populated with all the values the comprehension indicates (as a list would). Rather, it lazily computes the values as we need them (when its __next__ method is called).

In :
%timeit -n 10000 [x for x in range(10**3)]

10000 loops, best of 3: 38.1 µs per loop

In :
%timeit -n 10000 (x for x in range(10**10))

10000 loops, best of 3: 1.01 µs per loop


If a function takes an iterable, we can pass it a generator. This can be much more memory-efficient (and possibly more time-efficient) than passing the function a list with the same items (as would be returned by the generator).

In :
sum([x for x in range(10**3)])

Out:
499500
In :
sum(x for x in range(10**3))

Out:
499500
In :
%timeit 1024 in [2**x for x in range(100)]

10000 loops, best of 3: 47.6 µs per loop

In :
%timeit 1024 in (2**x for x in range(100))

100000 loops, best of 3: 5.23 µs per loop


A generator object can also be created by calling a generator function. A generator function is any function that contains the yield keyword.

In :
def foo():
yield

In :
foo()

Out:
<generator object foo at 0x10ee40620>
In :
g = foo()

In :
next(g)

In :
def foo():
yield 1

In :
g = foo()

In :
next(g)

Out:
1
In :
def foo():
print('foo was called')
yield 1

In :
g = foo()

In :
next(g)

foo was called

Out:
1

The body of a generator function is not actually run to yield a value when it is first called! It is only run for the first time when next is invoked on the returned generator object.

In :
def foo():
print('L1')
yield 1
print('L2')
yield 2
print('L3')
yield 3
print('L4')

In :
g = foo()

In :
next(g)

L1

Out:
1

Each next call on a generator object will run the body of the generator function up to the next yield statement (if any). The value specified in the yield statement will be the return value of next. If there is no remaining yield statement, the function will run until it exits and a StopIteration exception will be raised.

... which is just what we need to support iteration with a for loop!

In :
for x in foo():
print(x)

L1
1
L2
2
L3
3
L4

In :
def foo():
for x in range(10):
yield 2**x

In :
for x in foo():
print(x)

1
2
4
8
16
32
64
128
256
512


For our previous container type:

In :
class ArrayList:
def __init__(self):
self.data = []

def append(self, val):
self.data.append(None)
self.data[len(self.data)-1] = val

def __iter__(self):
for i in range(len(self.data)):
yield self.data[i]

In :
l = ArrayList()
for x in range(10):
l.append(2**x)

In :
for x in l:
print(x)

1
2
4
8
16
32
64
128
256
512