Iterating and Chaining
00:00
In the previous lesson, I introduced you to list comprehensions. In this lesson, I’ll show you some parts of the itertools
library. Of course, all towards flattening more lists of lists.
00:12
The itertools
module comes with Python, and as you might guess from the name, is full of tools for doing iterations.
00:20
Some functions provide iterators on infinite input. For example, count()
returns a steadily increasing number forever. You can give it a starting value as well.
00:31
Cycle()
iterates through its argument and when it reaches the end, cycles back to the beginning and does it again, and repeat()
gives you the same value over and over again, either infinitely or a given number of times.
00:44
There are also functions for modifying iterables as you go. Batched()
, which was newly added in Python 3.12, groups chunks of data together. This is the opposite of flattening.
00:55 You could take one of our flattened lists, batch it three items at a time and end up with the original list of lists.
01:03
Pairwise()
is kind of like batched
, but only for pairs. This one got introduced in Python 3.10. They could have skipped it if they’d gone straight to adding batched()
, and it’s essentially the same as calling batched
with the argument of two. And tee()
returns a number of independent iterators from a single one, effectively producing a series of iterables that each contain a slice of the original.
01:27
And the one I’m going to use to flatten lists of lists is the chain()
function. It allows you to treat multiple iterables as if they were a single iterable, chaining the iterables together.
01:39 Say you had two strings and you wanted to iterate over the letters in each. You could chain the strings together and get a single iterable back that processed all the letters in both. Python can get a little tricky sometimes. Things that you mentally model as functions sometimes aren’t.
01:57 When you use parentheses to call a function, what you’re actually doing is invoking a callable or a function. Invoking the callable is calling the function, but you can write special methods on classes so that you can do the same thing with an object.
02:11
The chain()
function isn’t really a function, but a class. And when you call it like a function, it invokes its callable method. Normally, you don’t have to think about this or care at all, you just use it.
02:23
But chain()
also has a utility method called from_iterable()
. This allows you to invoke chain
based on a single iterable containing iterables.
02:33
Using this method, you can call chain
on a list of strings or a list of lists, which is handy if you’re flattening. Let’s go see this in practice.
02:45
Back in flatten.py
, first, I need to import chain()
from the itertools
library, and then I call chain.from_iterable
method.
02:54
This chains the contents of nested
together. Notice that I’ve wrapped this in a call to list
, another case of a class as a callable actually. Wrapping it like this converts what comes back from the chaining call into a list.
03:09
What comes back from the chain
call is just an iterable. So to have an actual flattened list structure, you need to convert it into a list.
03:18 Let’s play with this. Deja vous, import vous.
03:29 And there you go. All flat and stuff. Itertools isn’t the only library that can help you flatten lists. Next up, I’ll show you functools.
Become a Member to join the conversation.