Python Tutorial: Sorting Lists, Tuples, and Objects

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody how's it going in this video we're going to be going over how to sort in Python so we're going to look at how we can sort a few different data types so we're going to go over sorting lists and tuples you can also run sword on dictionaries and you can also sort objects by a custom criteria but to get started let's go ahead and look at a couple ways in which we can just sort a simple list of integers so I have this list here called Li and it contains a bunch of random numbers between 1 and 9 so if we wanted to sort this how would we do it I'm going to make a new variable here called s underscore Li and I'm going to set it equal to and I'm going to run this sorted function and I'm going to pass in our original list to this sorted function so I'm going to save that and then I'm going to go ahead and print this out to the screen so I'm going to uncomment out this print statement here and print out this s underscore Li variable now if I run this you can see here that it did sort this list for us in ascending order so we have 1 through 9 here now let me go ahead and print out my original variable as well so I'm going to print out this Li list and if I print this out now you can see down here that my sorted variable is a sorted version of that list but my original is still the jumbled unsorted group of numbers now what if I wanted to sort that original list of numbers without creating a new variable well there I can use the sort method on the list so I can say Li dot sort and then just close off those parentheses and if I run that now you can see that we have our sorted variable here that is a sorted version of that and also when I ran this Li dot sort it also sorted the original list of integers also now one difference between this sorted function here and this sort method is that the sorted function returns a new sorted list so that's why we can set it to a variable but the sorted method just source the list in place and then returns none so this can be important because sometimes people get this confused and they'll try to so for example if I was to copy this and I was to try to set this s underscore Li to this Li dot sort if I was to do that and then run this you can see that instead of getting this sorted list we just get this nun value here so if you use the sort method then don't expect a list to be returned because it's just going to go ahead and do that in place okay so now we've seen how we can sort these lists and ascending order but what if I wanted these to be in descending order from highest to lowest well to do that we can just do pass in this reverse equals true and if I save that and run it now you can see that our new variable here is sorted in descending order and we can also use this exact same parameter with the sort method so if I pass that in the sort method then it does the original variable in place and descending order also okay so why would I want to choose the sort function or the sorted function over the sort method well that sort method on the list is is fine if you are working with lists and if you want to modify it in place but the sorted function gives us a little bit more flexibility because we can pass in any iterable as opposed to the sort method which works specifically on lists so for example here I have a tuple that has all the same values that we had in our list of integers and the tuple doesn't have a sort method so if I do a dot sort and run that then we get an error so what we have to do here is we have to use the sort function so I'm going to do a new variable and do a sorted I'm going to take off this method here since it doesn't have one and I'm going to save that and then I'm going to print out the new variable that we just created here so if I print this out save it and run it now you can see that our s underscore tub variable is a list of the values and our tuple that are now sorted so for another example here I have of a dictionary and you can pass a dictionary into sorted also so I'm going to do an s underscore the I equals sorted and pass in my dictionary there and then I'm going to go ahead and just print this out to the screen I'm going to comment these out now what sorted will do by default with this dictionary is it's just going to sort the keys so if I run this here then you can see that it returned a list with the sorted keys here so you can use either method of sorting that you like you can use either the sorted function or the sort method as long as you understand the differences but for the rest of this video I'm going to use this sorted function because I'm going to be working with examples other than lists so the sorted function is what you need for that so so far we've sorted integers in ascending and descending order but what if we want to distort values based on a different criteria so for example what if I had a list of integers here and I wanted to sort these based on their absolute value so you can see I have some negative values here now if I was just to print out this list right now and run this actually let me go ahead and sort this list and I'll show you what the default sort is for these negative values so I'm going to do sorted and pass in this original list and then I'm going to print out this s underscore Li variable and if I run this you can see that we got the exact same thing that it's in right now because these negative values are considered less than the positive values like you'd expect so we have negative 6 negative 5 negative 4 1 2 3 okay so like I was saying what if I wanted to sort this based on the absolute value of these values so it should be 1 2 3 4 5 6 so to do this we can use a key parameter so when I with are sorted here I'm going to pass in a key parameter and I'm going to say that the key equals ABS which we'll use the absolute value function now what this does is it runs each element through this absolute value function before it makes the comparison so now if I run it with this key in place now you can see that my sorted variable is 1 2 3 negative 4 negative 5 negative 6 now the key parameter is extremely useful when you're sorting objects with named attributes so for example here I have this very simple class called employee and within the employee here we just have a name and age and a salary and this re PR method here all it does is it tells Python how we want this function represented when it's printed out to the screen so now I have these three sample employees here that I've created with random names ages and salaries and I'm going to put all three of these employees into a list called employees and now I'm going to try to sort these based on a specific attribute so if I try to sort these without a key then it's not really going to know how to sort them so let me go ahead and try to sort them and print this out to the screen and you can see I get this type error here and it says inaudible uh nor durable types employee can't compare employee to employee so it's getting this employee object here from this list and it's saying hey I don't know how you want me to sort these so we need to use a key to sort these on and I'm going to write a custom function in order to sort these so remember our key takes a function that takes each element of our list and returns what we want to sort on so with the absolute value example that I used earlier I was able to use a built-in function for the key but for this example we need to write a custom function so the function I'm going to write is a very simple one it's just going to be F and let's say I'll do this e underscore sort and I'll pass in an employee and then I will just return and we'll a sort based on the employees name so I'll do EMP name so now where I ran this sorted function I can pass in this key and I'm just going to pass in I'm going to tell it to do the key as this sort function here so now if I run this then you can see that it returns all of our employees based on their name now if I wanted to sort these based on the employees age then it would be as easy as coming up to the sort function here and instead of returning the employee name I want to return the employee age and if I run that now you can see that it's the employee age in ascending order and lastly if I wanted to do this based on salary then I could go ahead and make this salary and save that and run it and you can see that it sorts it by the salary and ascending order now you can also still pass in your other parameters here too so if I did a reverse equals true and then ran this then it would sort in the descending order based on our key which is salary so now we have all the salaries in descending order now if you have a complicated sort function then it's probably best to break out these functions into separate functions like this but for those of you who are familiar with lambda functions then you probably noticed that something this short would be easy to turn into a lambda function so instead of setting this key equal to this Y dot sort I really could just do a lambda here of e now I'm not going to go into lambdas if you haven't seen these before then don't worry about it it's just a way to quickly write an anonymous function so if I wanted to sort on the Aged I'll do or the name I'll do a dot name I'm going to go ahead and take out reverse here and save that and run it and now you can see that it is sorted by the employee's name and ascending order and since we're using this lambda function here this our sort function up here that we used before isn't actually doing anything so I could still run that without this the underscore sort function now I should also mention that if you're working with attributes like this that you can import a function that specifically does this so I'm just going to do my import from down here so if I import the operator module and do a from operator import it is called adder get er this is it right here now what we can do with this is we can just use this as a replacement for our key so I'm going to take this lambda out here so I'm going to do an ad or Gitter and the attribute that I want I say I'll just do I'll do this as age so if I save that and run it now you can see that it's sorted based on the age so you might wonder why I just didn't show you that ad or get er right away instead of first showing you how to do these functions and the lambdas and things like that but knowing how to sort with these custom functions it'll allow you to be a little bit more flexible down the road when you want to sort more complicated items so I think that about does it for this video I hope it gives you some good ideas for how you can sort your list and objects and any way that you'd like but if you do have any questions just feel free to ask in the comment section below be sure to subscribe for future videos and thank you all for watching
Info
Channel: Corey Schafer
Views: 188,070
Rating: 4.9637399 out of 5
Keywords: Python (Programming Language), Python Tutorial, Python Sorting, Sorting in Python, Python Sort, Sorting Algorithm, List, Tuple, Object, Sort List, Sort Tuple, Sort Object, Custom Sort, Python for Beginners, Learn Python, Programming, Computer Science, Programming Tutorials, Software Engineering (Industry), Python List, Python Tuple, python sort list, python
Id: D3JvDWO-BY4
Channel Id: undefined
Length: 12min 7sec (727 seconds)
Published: Tue Dec 15 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.