Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Flattening a List of Lists in Python (Summary)

In this video course, you’ve learned how to flatten a list of lists in Python. You’ve used different tools and techniques to accomplish this task. First, you used a for loop along with the .extend() list method. Then you used other tools, such as list comprehensions and functions like functools.reduce(), itertools.chain(), and sum().

Then you ran a performance test to find out which of these tools offer faster solutions for flattening lists of lists. The test results indicate that your best options include using a loop and the .extend() method or the augmented concatenation operator.

With all this knowledge, you’re now ready to start flattening nested data in Python.

Download

Sample Code (.zip)

1.1 KB
Download

Course Slides (.pdf)

1.2 MB

00:00 In the previous lesson, I measured the performance of each of our list flattening functions. In this lesson, I’ll summarize the course.

00:08 A list of lists is a common data structure. The example I gave at the top of the course was left and right audio channels in a stereo signal where each sample of the left and right channel were a tuple, while the list held the whole signal. Flattening a list removes the inner nested structure.

00:25 In our stereo example, turning the series of tuples into a straight list of values.

00:31 There are many different ways to flatten lists. I started by showing you a simple approach that used nested iterators visiting each value in the inner lists.

00:40 This turned out to be relatively speedy, but not as quick as using a list’s .extend() or += method. By using .extend() or +=, you don’t have to visit the individual values you only need to merge the row list with the resulting flattened one.

00:54 The itertools.chain() function, which is actually a class, allows you to iterate over multiple iterables as if they were one, chaining the iterations together.

01:04 Converting chain’s response to a list object results in a flat list.

01:09 Functional programming is a style of coding where you manipulate functions, passing them by reference to each other and composing them together. The functools module includes the reduce() function, which is an accumulator. Passing different functions into reduce() changes how the nested lists are merged.

01:26 By choosing mechanisms that use in-place operations, you can achieve performance equivalent to the .extend() list algorithm. By choosing the wrong operator results in some very expensive code.

01:37 I also covered the use of the sum() function to flatten a list, not something I’d recommend, but it did serve to highlight the fact that adding two objects invokes the add() operation, regardless of whether that is adding numbers or merging lists. You’ve seen all sorts of ways of flattening lists of lists.

01:56 I hope you’ve learned something new along the way. Thanks for your attention.

Become a Member to join the conversation.