Using .extend()

00:00 In the previous lesson, I showed you what flattening a list means and introduced the nested iterator algorithm. In this lesson, I’ll use some of the features of a list class to take a more efficient approach.

00:13 Our first attempt at flattening a list used nested iterators. The outer iterator processed the outer list while the inner iterator appended each value in the inner lists to a result. This approach isn’t particularly efficient as it means you have to visit each item in the inner list.

00:31 List classes have methods that allow you to perform operations on the lists, like merging them, which you can take advantage of. This means you don’t need the inner iterator that visits each item, and it can be a more efficient approach.

00:45 Let’s go look at a better function. Throughout this course, I’m writing all of my functions inside of the same file. I’m doing it this way so later I can do some funky performance evaluation stuff.

00:59 So still in flatten.py, and this is my new function called flatten_extends(). Like with the previous version, it starts out with an empty list to store a result.

01:09 And also like the previous version, it iterates over the outer list. Here’s where it changes though. The list .extend() method takes the contents of a list and sticks it on the end of another list.

01:22 So here in one function call, all the values in a row get stuck onto the end of our result list. This one call replaces iterating over each of the values, which is how I had done it before.

01:34 The longer the inner lists are, the better this algorithm performs as it’s doing in one call what previously required a call for each item in the inner list.

01:45 This code’s even better than that because the list class code is actually itself quite efficient, and you’re letting the built-in library handle this rather than writing your own Python for doing it.

01:56 And as will happen in every version of our algorithms, the final thing here is to return the newly flattened list. Let’s try this out and make sure it works.

02:13 This is my nested data structure from before. I’ve just stuck it all on one line this time instead. Now I’ll import my new function

02:25 and call it. And there you go. A flattened list.

02:34 The list class implements the += operator. Operators in Python actually are syntactical shortcuts. When you say x += 3, in the background, Python is translating that into a method call that adds three more to x.

02:50 Any class can implement this same kind of method call, and in the case of a list, that implementation performs the same operation as .extend().

03:00 Let’s see this minor change in the code.

03:04 Back in my flatten.py file this time with a new function called flatten_concat(). The only difference between the concat version and the extends version is the use of the += operator.

03:16 This requires a bit less typing, but depending on how familiar other programmers are with this feature, it might make the code a little less clear. It’s sort of a style difference. Let me test it out,

03:38 and with the same nested data, you get the same result.

03:43 Next up, I’ll show you how to use a list comprehension to flatten lists.

Become a Member to join the conversation.