Flattening Lists

00:00 In the previous lesson, I gave an overview of the course. In this lesson, I’ll introduce you to flattening lists and show you the nested iterator algorithm.

00:10 Consider the mathematical concept of a matrix, a set of numbers organized in rows and columns. One way of representing this in Python code is to have a list of lists with the outer list being the matrix and each inner list representing a row. This kind of nested list within list structure or its sibling tuples within list is common in Python.

00:33 Sometimes though, you need to break the data out of the structure into a simplified list. This is known as flattening a list of lists. This, for example, is a flattened version of the matrix.

00:45 Note, this is a one-way journey. You’re losing the extra structural information, but sometimes you need to do this to perform other kinds of processing. Flattening a list is a common homework and interview question, but it also has purpose in the real world.

01:01 Frequently when doing data processing, source data is structured in a way that makes it inefficient to do processing upon it, and you need to perform a transformation before performing any actions.

01:12 Flattening is one such transformation. An example of this is with audio processing. A stereo signal consists of two parts of audio information, the left and right channel.

01:24 This often gets represented in code as a series of tuples, where each tuple is a pair containing the left and right channel sample, and then the pairs are stored in a list.

01:35 When it comes time to serialize this data into a file, the format needs to change. Audio formats don’t use Python tuples. For example, the wave audio format is made up of a series of fixed-sized bytes, alternating between the left and right channels, so to store it, a list of left and right channel tuples needs to be flattened before it’s sent as a stream of bytes.

01:58 This course covers a bunch of ways of flattening lists of lists and why you might choose a given algorithm. Let’s start out by writing some code consisting of nested iterations.

02:11 I’ve created a file called flatten.py. Inside it, I’ve written my first list flattening function. The function starts out by declaring an empty list, which is where I’m going to store the final result.

02:24 The code consists of a nested set of iterations with the outer iteration looping over the outer list. What’s in this outer list is inner lists, each of which I’m going to call a row, naming it after a row in a matrix.

02:39 Inside that, the inner iterator iterates over the values in the row itself. Each value then gets appended to my resulting flattened list. And then finally, I return the flattened list back from the function.

02:53 Let me open the REPL and test it out. First, I’ll declare a nested list.

03:06 Next, I import the flatten_loop() function from flattened.py.

03:14 And finally, I’ll call the function on the nested list.

03:20 Remember, the REPL prints out the return response of a calculation, and since I didn’t store the return response, it shows the flattened list directly here.

03:29 Let’s try another one. Maybe something a little bigger.

03:45 This one has a mix of things inside of it, integers, strings, and a float. Our flattening function doesn’t care though, as long as it’s lists inside a list the nested iteration approach still works.

04:00 There is one limitation, though. This algorithm only works on one layer of nesting.

04:14 Triple is still a list containing lists, but the second inner list also has a list inside of it. You can still run our function,

04:27 but the triply nested item is treated as a single thing. All the algorithms in this course will only deal with two levels of nesting. For more than that, you either need another level of iteration or, like in this case, where it’s mixed as to how many levels there are, you’d need a recursive processing algorithm, which is beyond the scope of this course.

04:49 You’ve seen the base case. Next up, I’ll show you how to take advantage of lists’ built-in methods to write some more efficient code.

Become a Member to join the conversation.