Join Barron Stone for an in-depth discussion in this video Multidimensional lists, part of Programming Foundations: Real-World Examples.
- Basic lists are fairly easy to understand because they're just a simple sequence of items. However, things can get a bit more complicated when we embed lists inside of other lists to create two-dimensional or even three-dimensional lists which can be a bit harder to visualize. That's one of the reasons I like to think of objects in a list as cars in a parking lot. To index my car in a two-dimensional parking lot, I need to know two pieced of information. First, I need to know which row I'm parked on and then I need to know at which spot along that row I parked.
The same concept can be extended to three-dimensional lists of lists of lists and that's like a multi-story parking garage. To index my car in there, I need to know three things. First, which floor I parked on. Next, I need to know which row on that floor I parked. And finally, which spot along that row, on that floor, I'm parked in. Now, my analogy has to end here because a four-dimensional parking lot doesn't exist in real life but software lists don't have to stop at just three-dimensions. You can embed lists inside of lists, inside of lists, inside of lists, inside of lists, inside of lists until your heart's content or your computer runs out of memory.
And how many layers deep you embed lists, that's how many numbers you'll need to index each object in that structure. Searching for an object in a multi-dimensional list is a bit more work. Finding an object in a multi-dimensional list is like trying to find your car in a parking structure when somebody else parked it. Hard. Some languages have fine methods to search for an object in a list structure, but in the background it's still systematically searching through every spot and every row on every floor in the list until it finds the correct object. If you've ever lost your car in a parking lot, you know how much of a pain that can be.
If the parking lot is small, it's not a big deal. But in a large lot, I can easily waste precious resources searching for my car. In this example script, called start 05 02 parking lots I've defined a two-dimensional list of lists containing car names. This is you're standard surface parking lot. The outer most list contains three other lists which each represent a row of parked cars. I formatted it on separate lines like this so you easily see each of the rows individually.
Below that, I've also defined a three-dimensional list of lists of lists which represents a multi-story parking garage. These cars are on Floor 0. These cars are on Floor 1. And these cars are on Floor 2. Notice that the various lists in the structure have different numbers of elements. We have individual rows which have anywhere from three down to no cars, which is an empty list. The list of lists even have different numbers of elements. For example, Floor 0 and 2 each have three rows of cars where as Floor #1 only has two rows of cars.
Now, lets run this script to load these lists into the Python Shell and try and indexing some cars. If I type lot 2d and hit Enter, then the shell will print out the contents of the two-dimensional lot. When it's formatted like this, it's easy to see that it's a list of lists with three individual lists of cars nestled inside of a larger list. If I index this list with a single value, let's say 2, then Python will return the element at Index #2 from the outer most list which is the list containing the Honda, Kia, and Mazda.
So, by providing only a single index value to the two-dimensional list, we're selecting an entire row of parked cars. To index an individual car from this parking lot, we'll need to know which row we're parked on and then, secondly, we'll need to know which spot on that row we parked in. So, I'll use the same command as before to retrieve the list of cars at Row #2 and then I'll use the square brackets again on that row to access the car at position #1. So, this line of code indexes out the car at Spot 1 on Row 2 which is the Kia.
The same concept applies to three-dimensional lists of lists of lists as well. If we give the multi-story parking garage a single index value, it'll return a two-dimensional list of lists with all of the cars on that floor. So, all of those cars are on Floor 0. If I give it a second index value of 2, then I get back the cars in Row 2 of Floor 0. And finally, if I give it three index values, 0, 2, 1, we'll get back the single car which is parked on Floor 0, Row 2, Spot #1 and that also happens to be a Kia.
Hopefully you recognize the pattern that as we add more index values, we're narrowing down what we're indexing as read from left to right. Now, let's say we want to go through all of the cars in this multi-story garage, accessing each of them individually and perform some sort of operation. Well, a common way to iterate through list elements in Python is to use a for loop.. So let's try that now. I'll type for car in lot 3d: Enter print car So in this case, the processing that I want to do is just print out the name of each of the cars in my 3-D garage individually.
I'll hit Enter twice to execute the loop and, no, that's not printing out individual cars. What it's done, is iterated through the outer most layer of the nested lists so that each lop iteration the object I get for car is actually the list of lists representing each floor. So instead of getting back individual cars, I've gotten back three two-dimensional lists and that's not what I wanted. So, how can I go through and access each of the individual cars within this parking structure? Well, since we have lists nested inside of other lists, one way we can unwind that is by using for loops nested inside other of for loops.
So, let's try that now. As we just saw, the first for loop will give us back each floor in the multi-story parking garage so we'll type for floor in lot 3d. Next, we'll want to iterate through each of the rows in that floor. So we'll create another for loop inside of that that says for row in floor. And finally, we'll want to go through and access each car in that row, for car in row: and it'll print out each car just like before.
So what this is doing is taking that three-dimensional parking garage. It's breaking it down and giving me back three, two-dimensional lists each one representing a floor. We're then going to operate on each of those floors and break those down into their component rows and then on operate on each of those rows and break them down into their component cars. Now, when I hit Enter twice to run that code, as you can see, we've gone through and used the Print function on each individual car in the three-dimensional list.
- Reusing functions
- Local vs. global variables
- Creating and naming custom objects
- Class inheritance
- Modules and packages
- Multidimensional lists and tuples
- Queues and stacks
- Creating and combining sets
- Storing data in dictionaries
- If/else and switch statements
- For vs. while loops
- Error handling
- Polling and event-driven programming