Join Barron Stone for an in-depth discussion in this video Parking with lists, part of Programming Foundations: Real-World Examples.
- Lists and tuples are two data structures that you can use to store a collection of objects in a specific order. You can visualize a list as a row of objects sitting in a line. Every object in the list is indexed by a number, and in the world of programming numbering starts with a zero for the first item, one for the next item, and so on down the line. You can use these indexes to insert, modify, and remove individual items from within the list. - Hey, you're back from the store. - Yup, and when I was there, parking my car, I had a moment of brilliance.
I figured out how I can explain lists. - What do lists and cars have to do with one another? - Olivia, that's a really good question. Let me tell you a story. In a lot of ways, you can think of a list of objects like a row of parked cars. The cars are in order, starting with the first car on the end, which is at index number zero. When I arrived at the store today, I decided to append my car to the list by parking at the end of the row. To remember where I had parked, I counted down the list of cars and determined that my car was at the fifth spot, which is index number four.
So I remembered that number and headed into the store. While I was inside the store shopping, other customers who came to the store also parked their car inside the list. Sometimes when a new customer arrived they would replace another car on the list because someone else was leaving. When a new car replaces another car at a specific index, it doesn't affect anyone else in the list. The list is just swapping one car for another. Similarly, some other customers who arrived were polite and appended their car into the list just like I did by parking at the end of the row.
When somebody does that, their car takes on the next index in the list and nobody else is bothered. - That makes sense. People are just coming and going to their cars. - Exactly. As long as new cars were either replacing other cars in the list or appending themselves to the end of the list, my car stayed at index number four. But a problem occurred when somebody showed up and decided that they needed to park at the front of the list. When that person inserted their car into the first position in the list, at index zero, it changed the index of all the cars behind that position because it shifted all of them down in the list.
This means that my car was no longer at index four, where I'd left it. When I came out of the store and looked for it there, I found somebody else's car. - So how'd you find your car? - Well, my car can be anywhere in the list, so I just had to look for it until I found it. I started my search at the beginning of the list and then I checked each car in order until I eventually found my car. If I had been parked near the front of the list, it would have been a pretty quick search. Unfortunately, I was parked closer to the end of the list, which means I had to search through a lot of cars until I found mine and that took a long time.
I also felt kinda bad when I left because I removed my car from the list and that meant everybody parked behind me got shifted up and had their index changed again. - Well I'm glad you eventually found your car. - Me too. You shouldn't expect an object to stay at the same position within a list if you're inserting and removing other objects because that can shift things around. For this demonstration, I'm just going to be entering commands directly into the Python interactive shell which I have open here through idle. One way to create a list in Python is to enclose a sequence of objects in square brackets with the objects separated by commas as shown here.
This line of code will create a new list object named "row" which contains four strings representing the cars that were parked at the store when I arrived there. When I arrived at the store, I parked my car at the end of the list. We can attach an item to the end of the list by using the append method, so let's append my Mercedes to the row of cars. Now, if I type "row", the Python shell will print out the contents of that list and you can see that the Mercedes has been appended to the end. My car is currently located at the fifth position in the list which corresponds to index number four.
We can access the item at that index in the list by using square brackets like this. If we want to replace an item in the list, we can do so by assigning the new object to a specific index location with the equal sign. While I was in the store, some of the customers who showed up at the store took the parking spaces of people who were leaving. Let's replace the BMW that's at index number two with a Jeep. Now if I display the contents of the row of cars again, you can see that the Jeep has replaced the BMW, which used to be at index two.
All of the other cars are still at their original locations in the list. Some of the other customers who showed up parked at the end of the row, so let's park another car at the end of the list using the append method. This time we'll make it a Honda. We check the list again to see if it's at the end, and I can check at index number four to see that my car is still where I parked it. Appending objects to the end of a list does not affect the index of any of the other objects that are already in the list. Now, if we want to represent the person who showed up and parked their car at the front of the list, we can do so by using the insert method.
The first argument for the insert method is the index for where we want to insert the new object. Since we want to put our car at the front of the list, we'll make the index value zero, and I'll make the car a Kia. If we check the contents of the list again, we can see that the Kia took its place at the front of the list, and all of the other cars were shifted down. That means that if I look for my car at index four, where I had left it-- Uh-oh, that's not the Mercedes I came to the store in. That's a Lexus parked in that spot. Since I'm not the kind of guy who steals a Lexus from the supermarket, I should figure out where my Mercedes went.
We can search a list to find the index of a specific object by using the index method. To do that, I'll pass my car, which is the string "Mercedes", to the index method. The index method searches through the list until it comes across the Mercedes object and then it returns the index where it was found, in this case, my Mercedes was parked at index number five. The index method starts its search at the beginning of the list and works its way down the row just like I did when I was searching for my car at the store. So, if the object it's searching for is near the front of the list, it'll find that relatively quickly, but if the object is located near the end of a really long list then that search process could take a lot longer.
Also, it is important to note that the index method only returns the first match that it finds because it stops as soon as it finds the object it's searching for. So, if there were several identical Mercedes objects parked in this row, I would have only received the index of the first one it finds. Now that I know my Mercedes is at index number five, I can take it out of the list by using the pop method. Pop will remove the object at the specified index, so I'll give it five, and it will return that object, as you can see, I get my Mercedes from the list.
And I'll display the contents of the list so you can see that the Mercedes has been taken out. There's one other list method that I want to show you and that's the remove method. Rather than using index to find the index of my car, and then using pop to take it out of the list, I could have just used the remove method, which basically combines those two actions into one routine. So let's try using the remove method to get rid of the Lexus. Unlike the pop method, when I use the remove method, it does not return the object that was removed from the list and that makes sense because I told exactly which object I wanted to be removed.
So I already knew what it took out. Now, if I display the contents of the list one final time, you can see that the Lexus is no longer there.
- 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