Dive into mastering Python list methods through practical repetition and clear examples. Reinforce your understanding by tackling a hands-on review challenge designed for effective learning.
Key Insights
- Reinforces understanding of Python list methods such as
extend
,append
,insert
, andremove
, with clear examples like adding multiple berries withextend
and removing "kiwi" from a list by name. - Stresses the effectiveness of repeated exposure and interactive practice to internalize coding concepts, noting that fluency requires multiple encounters beyond initial explanations and demonstrations.
- Highlights practical techniques for manipulating lists—such as using sets to remove duplicates and inserting a previously removed item ("grape") back into its alphabetical position without resorting, by leveraging the
index()
method.
Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.
This is a lesson preview only. For the full lesson, purchase the course here.
We're going to work some list method reviews into this lesson. List methods—there are a lot of them, and that was Lesson 1, so we've got to revisit things. The goal of the list method review is to make our list a little longer and just move some stuff around.
Take the opportunity to run a few list methods on this list, and then we'll get back into the looping. This would be a review challenge that I'd like you to do. You might have to go back to Lesson 1 to look at how to implement these various methods.
You have to see everything more than once. Really, the first time you see anything that I show you, it doesn't matter how clearly it's explained. I mean, it does matter actually.
It helps that everything is clearly explained and documented and gives you the opportunity to type, and I'm always telling you, please type, and you've got the explanations, little challenges. But even then, even so, the first time you see anything, it's more like just putting it on your radar screen so that you know it exists, it's a thing. To get fluent with this, you have to see it again, so this is what this is about.
The second pass, it'll hit better. Extend—we're going to use the extend method to add three berries to the end of the list while keeping the list flat. B, this is all for you to do.
I'm just going to read them aloud and then pause and let you tackle this. B, we're going to remove—it'll take a while, pause as long as you need. I mean, 10,15,20 minutes, whatever it takes.
B, remove by name—remove the kiwi by name. Append the final fruits one by one; just don't use extend. Put the kiwi back and also append grapefruit.
Then do an insert. I would like you to put papaya at the beginning of the list, like the opposite of append. That's insert(index, item).
Pop at index—just pop whatever item is at index 5; that'll probably be grape at that point. You can also save the result. Pop returns the popped item, which you can save.
You would say: item = list.pop(). You would get the item. It would come out of the list, but you would have it as a separate item. Remove duplicates.
Oh, yeah. Let's do this twice so that we have duplicate berries. Okay, that's why we had it twice.
We want duplicates. And why do we want duplicates? Because we want to remove the duplicates. By turning them into a set—remove duplicates by making a set from the list and then back to list.
That's the move. The making of the set from the list purges the duplicates, but you don't want a set, right? Because they're not ordered. They don't have an index.
They've got the squiggles. Then you pass that to list. It's kind of like list → set → list, like game, set, and match: list, set, list.
Then alphabetize the list. Put the grape back right before the grapefruit without having to sort and without knowing where the grapefruit is. So you're going to use index.
You're going to have to use list.index(item) to find the index of grapefruit. If you want to put the grape back that you popped—assuming that's the popped item, it should be—you would say: okay, I’ve got the list alphabetized, right? Everything’s in alphabetical order.
And I want to put the grape right in before grapefruit to keep it alphabetized without having to resort it. It's just good practice. So you would have to look up the location of grapefruit, get its index, and then put the grape in at that index.
So the grape would become—the inserted grape would take grapefruit's index or would go in the grapefruit's index, which would push grapefruit down by one. It would just nudge the whole thing down. All right, that's a lot of stuff.
These are all review of list methods. Pause, please. Give it a good effort and come back for the solution.
You'll learn it better if you tackle these challenges. Okay, we're back for the list method review challenge solution. Extend—add three berries to the end of the list while keeping the list flat.
We'll say fruits.extend, and you've got to listify the three items you're putting in. I mean, we could make a separate list. We could say berries = […], if you like, and then we could extend with berries if that's easier to read.
We could also pass in the raw list directly, right, with no variable assigned to it, like that. That's adding it twice. If you print fruits, you're going to have those three berries at the end twice.
There they are. We should probably pprint this. Pprint, pprint.
You've got to have four Ps for that. There it is. That's why you pprint it.
All right. Oh, wait. Now we've got it four times.
Okay, because I ran it again. Actually, it's a little problematic to do this in multiple cells, because if we do it one by one and keep running, we're going to keep adding. So what we want to do is just do each of these as its own cell so that once we add the three berries twice, we don't add the three berries again twice, right? Every time you run the cell, it'll run everything in the cell, not just the new move.
We're going to put everything in its own little box. And just for good measure, rerun fruits one more time. All right.
Boom, there it is. Okay, so now we've got cranberry, blackberry, boysenberry, cranberry, blackberry, boysenberry. Why would we want them twice? Well, so that we can purge the duplicates, right? We want duplicates.
All right, so that's A, and that's the review of extend. Also, note that you could pass extend a variable or just the contents—just the actual list raw with no variable name. All right, we're going to remove by name—remove the kiwi by name.
We'll say fruits.remove("kiwi"). And if we print fruits, there should be no kiwi. I'll pprint it. Right—kiwi, it was grape, kiwi, lemon.
And it'll throw an error if you try to remove something that's already removed, I think. So let's—right—grape, lemon. Right? No more kiwi. Now, if you don't want to see everything, you might just want to see enough items to know if it worked. And I know that kiwi was one of the first seven or eight items.
We could also just output a sample, like a slice. So I'll say start at zero and go to seven. Zero, one, two, three, four, five, six, seven.
Eight maybe, to get—because it's exclusive. Whoa. Oh, see, it doesn't work. Remove(X): X is not in the list.
If you remove kiwi and then you run it again, it breaks because there is no kiwi, which is why we don't want to be rerunning these cells. There, I just wanted to show you that when you print fruits, you wouldn't have to print the whole thing. Remember, we can print a range.
We can start at index zero and go to index eight exclusive, getting zero through seven. We just need to see enough to know that the kiwi is no longer sitting between the grape and the lemon. Okay, append the final fruits one by one.
Fruits.append("grapefruit"). Append is such an important move. It's probably the one you just keep doing—probably a third of your array methods are appending.
I'm not going to run the cell again. If we want to see the fruits, we have to run it separately because I don't want to append twice.
There they are. Now, if I just wanted to see the last few to know if it worked, we could say: just show me the last five. There you go.
Wait a second. Last five fruits: grapefruit, kiwi. Right? Aren’t those the last five fruits right there? Right, fruits. Oh, it’s negative five to the end. That’s what we want.
That’s how you do it. Okay, yeah, there we go. I had the wrong syntax.
Just check the last five to see if two new items are at the end, right? We don’t have to print everything. Just print the first eight items to make sure kiwi is gone. Don’t run this again, though, unless you want to start over. Remove kiwi will give you an error if you run it again because there is no more kiwi.
So be careful when running these additive and destructive operations. Additive—if I run this cell again, append it, I’ll get another grapefruit and kiwi. All right, let’s try that.
There: grapefruit, kiwi, grapefruit, kiwi. So you have to be careful about that. And if you run fruits.remove("kiwi") again, well, that’ll actually work now because what it’ll do is remove the first occurrence it finds.
Let’s see. It’ll still leave the kiwi at the end, I’m pretty sure. It’s not going to remove all of them, I don’t think.
Let’s see. Yeah, see? It got rid of the first kiwi it found. So anyway, if you do removing—if you run a cell that adds or removes—it’s going to add or remove.
If you only want to run that cell once, then keeping it by itself is the best solution. That’s why I broke all these up into separate cells, right? We had A through G all sitting in one cell, and I separated it for that reason.