Master Python list manipulation by learning how to insert, pop, remove duplicates, and maintain alphabetical order with ease. Discover practical techniques to manage data structures efficiently for smoother coding workflows.
Key Insights
- Learn how to insert an item at a specific index using Python's
insert()
, demonstrated by adding "papaya" at index 0 to a list. - Understand the use of sets to effectively remove duplicates from a Python list, though with the trade-off of losing the original order and index positions.
- Explore sorting and indexing methods, such as finding the index of "grapefruit" within a list to accurately reinsert "grape" and preserve alphabetical order.
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.
All right, next on to D, we're going to insert a new item, papaya, at the beginning, which would be index 0. We'll say fruits.insert at 0, papaya. This is kind of like append from the beginning. Run that.
And then let's just check if papaya is at the beginning. Now, you could just check fruit 0 for that matter, or you could do a little range for context. There you go.
Pop index. Let's pop the item at index 5, whatever it may be. What is the item at index 5? It is grape.
OK, that's what I was trying to maintain that, right? The idea is it's grape. Whatever it is, it should be grape. Not whatever it is.
It should be grape, because we're going to try to put the grape back. Pop returns the popped item. We'll say popped grape, or just popped item, equals fruits.pop 5. And then we will print the popped item.
And then let's make sure grape at index 5 is no more. Yep, it worked. There's the popped grape, and there's no more grape at 5. Used to be cherry grape lemon.
Now it's cherry lemon lime to finish through index 7. Exclusive, right? 0, 1, 2, 3, 4, 5, 6. Remove duplicates by making a set from the list, and then write back to list. We'll say fruits set. Let's just look at it step by step.
Would be set, game set, and match on your fruits. Actually, we probably want to pprint that. Let's just do it one by one.
OK, fruits set. Let's see the whole thing. There it is.
With curly braces instead of square brackets, and with all the duplicates gone. So those extra berries that we had in the extra grapefruit, gone. And there's no index.
You can't say show me the first five of these. Watch. See? Set object is not subscriptable.
Let's leave that as a note. Sets do not store items by index position number at all. OK.
So let's do it all in one move now. We'll say fruits. We're going to get rid of the duplicates.
We'll say fruits equals list. If we just print fruits again, and by the way, if you just want to run, you don't have to print. You can just run the item like that.
The reason you need to print is if you're trying to run something else after it. We have the original fruit still has duplicates, right? Go down the end, you see all these duplicates. We'll just purge them by setting fruits equal to itself.
List set fruits. We're going to take fruits, pass it to set. That'll get rid of the duplicates.
But we don't want to set, right? We do not want a set because we don't want the squiggles, and we do want it to be subscriptable. We want to be able to go by index. We want a list.
We even like the square brackets. So how do you get back to a list after you've taken out the duplicates? Well, the set is just a stepping stone. It's just a move you do to get rid of the duplicates.
You want a list though. So you take that set, and you pass it right back to list. And it's perfect for making a list out of it because it's got multiple items.
And run that, and then we could go ahead and print. And if we do the last seven, that was where all the duplicates were found. Just make sure list has no more duplicates, which were all the way at the end.
We're just going to go look at the last seven. There, no more dupes. Oh, it's all scrambled though now because it had been made into a set.
We just print everything. When you make a set, not only does it not have an index, like the items are not stored by position, it doesn't even care what order they were in when the set took the list. It just kind of scrambles it and throws them out there as if to say, as if to really emphasize and drive home the idea that there is no index.
If we want to see all of them to comb for duplicates, we kind of have to just print it all here. All right. Let's sort the list, which is all scrambled now.
We'll say fruits.sort, want to alphabetize that. Boom, there you go. And we could just do the first handful, right? If we wanted to verify that it's alphabetized, could probably be OK just printing some of them.
Yeah, that looks alphabetized. All right, now let's put the grape back right before the grapefruit without having to sort again. OK, so we don't want to just put the grape at the end because then we would have to sort everything.
We want to find out where the grapefruit is. So let's, there. There's the grapefruit.
So maintaining alphabetical order, we would want to find the grapefruit and then put in the grape at that index, which would push the grapefruit and everything else down by 1. 0, 1, 2, 3, 4, 5, 6, 7. We'd want to put the grape in at index 7. But we don't know that. We have to find the grapefruit index. We'll say index of grapefruit equals fruits index grapefruit.
And let's print that. And it is 7. Now what we want to do is put the grape in at 7 using that dynamic value index of grapefruit. We can say fruits.insert index.
And we want to put the grape in there. And it does not matter, single double quotes. Notice I'm just kind of using them interchangeably.
And let's make sure it worked. Yep, grape went in in the right spot. Make sure grape went in at the correct position to maintain A-to-Z. All righty.
So that is all straight up review. I hope that was helpful. We need review.
And it can't just be, oh, on your own, go review this. That's not the same. We got to systematically do it as group exercises.
So thanks for toughing it out on that. Let's move on. Back to loops.
We got more items now, too, to loop.