- Read Tutorial
- Watch Guide Video
Let's walk through a basic example and reiterate what sort is doing. So if I come here and I say sale_prices.sort
and then come and print out sale prices this is going to sort that list. And as you can see it goes from one to 400 with these lists of integers and that all works.
However, you may notice we're actually calling the original list. And so what that means is simply by calling the sort function on sale prices we have changed the entire structure of these items and that maybe something that you want to do.
However, there may also be a time where you simply want to pass this to a variable and then have that variable perform that sorting. And as we've already walked through if I create some means such as sorted_list I create a variable like this and try to print this out. What's going to happen is it's going to print out none and that is because sort does not return value.
So what do you do when you actually want to return that?
Well, that's what this guide is going to be about. There is a method called sorted that we can utilize that has the same type of behavior as sort except it allows you to actually store that value. That new sorted value inside of a different variable. So what this means is sorted does not change the values in place so the list is going to remain completely intact and anyone else any other part of the program that didn't expect a sorted list is going to be able to use it.
Now, this may not seem like a big deal to you if you have not built a ton of different applications than the concept of if a list is sorted or if it isn't may seem pointless. However, there are algorithms that specifically expect a list to be either sorted or unsorted, and if other parts of the program are making decisions based on the expectation that a list is going to be random then that could have negative consequences on the entire system. So it's important to keep that kind of in the back of your mind. This may not really play a big part in your development choices until you get into more advanced kinds of development techniques. However, I would be doing you a disservice if I wasn't teaching you all of the different variations that you can utilize so that when it comes to that point where you need to. Just like in this example have a sorted list then you can do that.
So let's see how we can do that and how we can use the sorted function. So this is going to be a little bit different we're not going to call it from the back we're going to call it from the front.
I'm gonna say sorted and then I can pass this in and I pass sale prices which is this list I can pass it in as an argument. And so now if I run this I don't get none anymore.
Now I actually get the full sorted list and it's working properly now to make sure that we have not to change the values here. Let's actually print out our sale prices. So I'm going to hit return and now you can see sale prices now is completely intact.
It is now something where we can trust that we didn't change the order of those values and the rest of our program can be more confident that those values have not been altered.
Now if you remember one of the very helpful little tools with the sort function is that you can store items in ascending or descending values and sorted you can perform the exact same task. So if I come here and pass in a second optional argument of a reverse equals true then this is going to perform the exact same task as with sort.
sorted_list = sorted(sale_prices, reverse=True)
And I'm going to delete sale prices since we know that that is intact return and now you can see it is sorting in a top to bottom order
where it's taking the greatest values and it's putting them there first. So now you should have a good idea on multiple ways that you can store a list in python using the sort function and the sorted function.
Code
sale_prices = [ 100, 83, 220, 40, 100, 400, 10, 1, 3 ] sorted_list = sorted(sale_prices, reverse=True) print(sorted_list)