Discover how Python sets can efficiently eliminate duplicates in your lists. Additionally, explore sorting, indexing, and performing vector operations to enhance your programming skills.
Key Insights
- Use Python sets to remove duplicates from lists by converting a list into a set, then back into a list, as sets store only unique values without maintaining order.
- Perform common numerical operations like sorting, summing, finding minimum, and maximum values directly on lists using built-in Python functions such as
sorted()
,sum()
,min()
, andmax()
. - Combine and repeat lists easily through vector operations, using the plus
+
operator to join lists and the multiplication*
operator to replicate list elements.
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, now a set is a list-like structure but having unique values. So items in a set are all unique. Items in a set are wrapped in curly braces, not square brackets.
Items in a set are not stored by any kind of index. Items in a set are printed randomly, so if you turn a list into a set, it might scramble the values because it just doesn't care about the order. And if you take a list and pass it to the set function, it will return a set.
Now, if your only purpose was to get rid of duplicates, you could take a list, pass it to the set, and then immediately pass that to list to return a new list of unique items. So that is a way to remove… this removes duplicates. This is the little trick for removing duplicates from a list and keeping it as a list.
So let's look at how that works. We'll say pets again, and now we've got triple bunnies as a breed. Those pesky bunnies.
What we're going to do is we're going to get rid of… let's purge the duplicates. So my set equals set. My list returns a set from a list.
So let's just get the unique pets by making a set. We'll call it pet set equals the jet set. All right.
Set, pets, pet set. Okay, so only one bunny, right? Because pet sets are unique, values only. But it also kind of scrambled stuff.
Put the cat at the end. It put the bunny in between the dog and the cat. That makes sense.
I don't think the dog and the cat want to be next to each other. But it also put it in curly braces, right? It's not a list anymore. And what you'll notice too is if you try to access item at, say, zero, it's going to give you an error because object is not subscriptable.
You can't go look up items in a set by index like you can with a list or a string. There is no index, which is why I didn't care about the order of the items. Now, let's start over and let's get the data type of this.
We'll say type pet set. We'll know that it's a set, right? Yep. But we don't want a set.
The set is a stepping stone. The set is just a way of getting rid of the duplicates. We really want it to be a list.
So what you do is pets, no duplicates, or let's call it unique pets. We're going to start with so pets itself still has duplicates, right? And then pet set doesn't have any duplicates, but it's not a list. We want to say unique pets list equals set pets.
Now, we could do this. We could say list pet set and then print, and it will take you back to a list, right? We passed the list to sets to get a set with no duplicates, but we're taking the set with no duplicates, passing it to list to get a list with no duplicates, which was the goal, say, right? The goal being I want to get rid of all the duplicates in my list, just have unique items. But you can do it all in one move, all in one line, with nested list set list, like so.
We're going to say unique pets list is going to equal set pets. We're taking the pets list with the duplicates, passing it to set to get rid of the duplicates, taking that and passing it to list to get back to being a list of no duplicates. And that works.
Sorting numbers is just like sorting strings, okay? We're just about done with this file, believe it or not. All right. We're going to say nums.
Mostly we're dealing with numbers. When we get into the data science stuff, a lot of numbers, very, well, there's strings, right? There's data. Of course, think of spreadsheets, right? Lots of stringy values, last name, and all this other stuff.
We're going to take the numbers and sort them. And we'll print the result. And that is sorted in ascending order.
Okay. Let's pop by index. Let's get rid of the item right before the 18.
We would have to find the item of the 18, right? We're going to say index of 18 is going to equal nums index 18. We're going to find whatever that number is. The index of the 18 is four.
Now, if you want to get rid of the item right before that, you would get rid of the item at three. We'll say nums.pop. I'm not going to write three, right? We're going to say minus one. And then if you print nums again, you'll have no whatever was before the 18, right? The five is gone.
Five was before the 18. Now it is gone. No more five, right? No more five.
It's gone. And there ain't nothing we can do. It's gone.
We had a little problem at the house. It's gone. All right.
Insert ferret as first item in pet's little wrap up. Okay. First item.
Now, how would you do the first item? Now, this is not a wrap up. This is actually a new method. We haven't looked at insert.
So append would put it at the end. Insert, this is a wrap up as far as we're done with the methods. Well, we did copy.
So this is, yeah, let's put it back. Let's put it in order that we did them in. Okay.
The last one we're looking at here is insert. Insert takes two arguments, an index and an item. So I want to go at index two, say, and put in whatever, you know, put in a potbellied pig or something.
So ferret. Fair enough. Pets.insert at index zero, right? The first item.
Kind of the opposite of append. We're going at the opposite end. And you'll get a ferret.
Now, let's insert zero at its correct position at index two. We'll say nums.insert index two, zero, right? So it's going to go negative one and four. And there it is.
There's the zero at index two. There are some other functions, sum, min, and max, where you pass in lists and it'll return the sum of all the numbers, the min of all the numbers, and the max of all the numbers. If you want things like the mean or the median or the mode or all these other statistical values, we have to import libraries for that.
But just raw, you know, out of the box Python, we have this sum, min, and max. So let's say nums.sum. Sum of the nums list is sum nums. And it's 201.
Take their word for it. Let's get the min and the max. We'll say nums.min equals min nums.
And nums.max equals the max nums. Negative three and 78. Looks right.
There they are. Now, if it's a sorted list, you could also just get them at zero and negative one, right? You know what I mean? We could also say, or the min would just be num zero and the max would be nums negative one. But you'd have to, they'd have to be sorted for that, right? Or get at index zero and negative one, but that requires sorted in ascending order.
Or get min and max at index zero and one respectively. Yep. Boom.
All right. A vector operation. Last thing.
You can add two lists together with a plus sign. Other programming languages don't really support that. It's called a vector operation.
Vector being this one dimensional structure like a list. We can actually just add lists together. You don't have to use extend, which we did before to get another list tacked on to the pets.
We could say, let's call it extra pets, more pets, something like that. And print more pets. There you go.
Now what we're going to do is add them together. We'll say pets equals pets plus more pets. Now you got all those pets.
Oh, we want unique pets though, right? We don't want three bunnies. Whoops. Oh, it's unique pets list.
There you go. All unique. It's not unique though, because I did it twice.
Restart session and run all. But the third time I've had to do that, it is a good move to know. I ran the thing twice, so I got the pets twice.
All right. 10%. What's up? There we go.
Okay. Yeah. It's going to make me do it again.
I don't want to do the whole thing over because it's going to make me type. That's the problem. It's going to make you type.
If you do have to rerun everything, it's fine unless you've got inputs that it'll spin on and make you do. You can't just get right back to where you were at the end. It's going to make you do the inputs every time.
So I want to avoid doing them, so I'm just going to run through here. This kind of stuff happens, by the way, to the best of them. I've been doing this a while.
It's just inevitability. You understand what happened, right? I ran an additive cell twice, so I got extra pets, and the only way to really get rid of them was to run all again. But in so doing, I was forced to input the food and the Bev on those previous challenges.
And once that was done, it won through an error and it wasn't it was just made more sense to just do this, and we're almost back. Vector operations can also be made with multiplication. So let's say nums equals nums times three.
There you go. You get them three times. Get every item three times.
Pets. Here we'll go more pets equals more pets times two, let's say. We'll just double them up.
There you go. Turtle, canary, goldfish, ferret. Turtle, canary, goldfish, ferret.
That is a vector operation on a list doing math across lists basically. You take a list, multiply it by two, you get doubles. All right.
That, well done. Congratulations. That is finally the end of lesson one.
The other lessons, lesson seven is really long, but this is the longest one till then. So congrats for getting over this hump. And that's just variables and some operations on them, right? We haven't gotten into the next lesson.
We get into conditional logic, then we get into loops. And so, all right, hang tough. Gotta know this stuff.
There's no two ways about it. So thanks for hanging in there and we will see you in lesson coming right up. Or whenever you are ready, we'll be here for you.