Iteration Tools · Lecture 05 · 14 min
Chaining and Teeing Iterators
chain glues several iterables into one stream (lazily, if you use chain.from_iterable), and tee splits one iterator into several independent ones.
Concatenating iterators
Sequences can be concatenated with +: [1, 2] + [3, 4]. Generators can’t, because they have no __add__. Yet we often need to treat several iterables as one long stream, for example processing three log files as if they were one. Here are three generators:
def make_gens():
return ((i ** 2 for i in range(4)),
(i ** 2 for i in range(4, 8)),
(i ** 2 for i in range(8, 12)))
l1, l2, l3 = make_gens()
for gen in (l1, l2, l3):
for item in gen:
print(item, end=' ')A nested loop works, but it has to be repeated everywhere we need the combined stream. Wrapping it in a generator function gives us something reusable:
def chain_iterables(*iterables):
for iterable in iterables:
yield from iterable
print(list(chain_iterables(*make_gens())))itertools.chain
That generator is exactly what itertools.chain does (implemented in C):
from itertools import chain
l1, l2, l3 = make_gens()
print(list(chain(l1, l2, l3)))
print(list(chain('ab', [1, 2], range(3)))) # any mix of iterableschain takes the iterables as separate positional arguments. If you pass a single list of generators, it chains just that one iterable, so you get the generators themselves back, not their values:
gens = list(make_gens())
for item in chain(gens):
print(item)The quick fix is unpacking, chain(*gens):
print(list(chain(*make_gens())))Unpacking is not lazy
Unpacking with * has a cost that’s easy to miss: Python must iterate the outer iterable completely to build the argument tuple, before chain even starts. That’s harmless for a small tuple, but if the outer iterable is itself a lazy generator (say, one that opens a file per item), unpacking forces all of it up front. Watch the order of the prints:
def squares():
print('producing 1st generator')
yield (i ** 2 for i in range(4))
print('producing 2nd generator')
yield (i ** 2 for i in range(4, 8))
print('producing 3rd generator')
yield (i ** 2 for i in range(8, 12))
c = chain(*squares())
print('chain created')All three were produced before chain existed. An infinite outer iterable would never finish unpacking at all.
chain.from_iterable
The alternate constructor chain.from_iterable(iterable_of_iterables) takes one argument and iterates the outer level lazily too, pulling the next inner iterable only when the previous one is exhausted:
c = chain.from_iterable(squares())
print('chain created')
for item in c:
print(item)Now the “producing” messages are interleaved with the values. A hand-written equivalent is just as short as before, with the * moved:
def chain_from_iterable(iterable):
for item in iterable:
yield from item
chain.from_iterable is also the idiomatic way to flatten one level of nesting:
matrix = [[1, 2, 3], [4, 5], [6]]
list(chain.from_iterable(matrix))“Copying” an iterator with tee
The opposite problem: you have one iterator and need to go through it more than once, maybe computing a running total in one pass and a maximum in another. Assigning it to a second name doesn’t help; both names refer to the same iterator object, and draining one drains “both”.
itertools.tee(iterable, n=2) returns a tuple of n independent iterators over the same data:
from itertools import tee
def squares(n):
for i in range(n):
yield i ** 2
iters = tee(squares(10), 3)
print(type(iters), len(iters))
iter1, iter2, iter3 = iters
print(next(iter1), next(iter1), next(iter1))
print(next(iter2), next(iter2))
print(next(iter3))Each one advances on its own. Under the hood, tee pulls each value from the source once and keeps it in a shared buffer until every copy has seen it. Two consequences follow:
- If one copy races far ahead of the others, everything in between stays buffered in memory. When you know you’ll consume one copy entirely before starting the next,
list(iterator)is simpler and no worse. - After calling
tee, stop using the original iterator. Pulling from it directly takes values the copies will never see:
source = iter(range(6))
a, b = tee(source)
print(next(a))
print(next(source)) # bypasses the tee buffer...
print(list(a), list(b)) # ...so 1 is missing from both copiesAlso note that tee always returns iterators, even if you give it a list. Each copy is single-use:
l = [1, 2, 3, 4]
lists = tee(l, 2)
print(lists[0])
print(list(lists[0]), list(lists[0]))
print(iter(lists[1]) is lists[1], '__next__' in dir(lists[1]))Pairing each item with the next one is done by teeing, advancing one copy, and zipping the two. Python 3.10 added this as itertools.pairwise, covered in the Zipping lesson.
def pairs(iterable):
a, b = tee(iterable)
next(b, None)
return zip(a, b)
list(pairs('ABCD'))Key takeaways
chain(a, b, c)yields everything froma, thenb, thenc: concatenation for any iterables.chain(*outer)must exhaustouterup front;chain.from_iterable(outer)stays lazy at both levels and is the standard way to flatten one level.tee(it, n)givesnindependent iterators that share a buffer; don’t touch the original afterwards, and beware of memory when copies drift apart.teealways returns one-shot iterators, whatever the input.