How to Sort Lists in Python - Python Tutorial for Absolute Beginners | Mosh

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
here we have this list with a bunch of numbers that are not in any particular order to sort this list we call numbers that's sort now let's print it and you can see our numbers are sorted in ascending order what if you want to sort these items in descending order well this sort method takes two parameters key which we'll look at later and reverse which we can use to change the sort order so here we can use a keyword argument to set a value for the reverse parameter we set this to true and with this you can see our numbers are sorted in descending order so this is the basic of sorting lists however in addition to the sort method we have a built-in function called sorted as you can see this function takes an iterable so we can pass any eatables here and it will sort it for us so we can pass our numbers list and this will return a new list that is sorted so unlike the sort method this will not modify the original list it will return a new sorted list so let me comment out this line and print the result of calling this function now let's run this program you can see here is our new list that is sorted and below that we have our original list that is not sorted also similar to the sort method if you want to change the sort order you can simply set the reverse argument to true let's run it one more time and here's the result beautiful so sorting numbers and strings is pretty easy but what if you're dealing with a list of complex objects for example what if we have a list of tuples let me show you so I'm going to delete everything here let's imagine we are building an application for processing orders and we have this list of order items every item in this list is a topple with two items the product name followed by the so here's the first item now let's add another item with a different price and finally the third item what happens if we try to sort this list let's take a look so items the sort and then we print the items so nothing is changed here because python doesn't know how to sort this list in situations like this we need to define a function that Python we use for sorting lists let me show you so here I'm going to define a function let's call it sort under line item this function should take an item like this topple there you go and it should return a value that will be used for sorting in this example let's imagine we want to sort these items based on their price so if each item is a topple we can get the price using square brackets of one so we'll return item of one so all these function does is that it takes an item and it returns its price now Python is dealing with a list of numbers and it can easily sort that list now finally we need to pass this function when sorting our list of items so once again look at this sort method the first parameter is key and this is where we need to pass our sorting function so we pass sort underlying item but note that I'm not calling this function I'm simply passing a reference to this function when python attempts to sort this list the list of items it gets each item and it will pass each item to our sort function okay so let's run this program and see what happens we get a type error sort takes no positional arguments which basically means here we can only use keyword arguments so we need to specify our argument that is key we set it to sort item now let's run this program one more time okay look our list of items is sorted by the price of each item however the we have implemented this sort function is a little bit ugly there is a better way and that's what I'm gonna show you next if you have any experience with other programming languages you've probably heard of the term lambda expression or lambda function it's basically a simple one-line anonymous function that we can pass to other functions so here on line 12 we're passing a reference to our sort under line item function we can improve this code and make it cleaner by using a lambda expression or an anonymous function so we don't have to define this function first and then pass it here so we remove this and add lambda with this per telling Python that we're defining a lambda or anonymous function the syntax for writing a lambda function is like this we add parameters colon expression so using this syntax we can rewrite this function how many parameters do we have here only one so we add that here item then after the colon we should add an expression what are we returning in our sort under an item function we are returning item of one so we simply add that here item of one now you can see with this syntax we don't need to use def for defining a function we don't need to give our function and name we don't need these parentheses here and we don't need the return statement so this is a shorter and cleaner way to define a function that we're going to use only once as an argument to another function now let's delete these two lines run our program again we'll get the exact same result our items are sorted by their price hi guys thank you for watching this tutorial my name is Mohamad ani and I have tons of tutorials like this for you on my channel so be sure to subscribe and also please like and share this video if you want to learn Python properly from scratch please depth I have a comprehensive Python tutorial for you the link is below this video so click the link to get started thank you and have a fantastic day
Info
Channel: Programming with Mosh
Views: 22,673
Rating: 4.9402986 out of 5
Keywords: learn python, python for beginners, python lists, python tutorials for beginners, python basics, absolute beginners, python list, python tutorials, python tutorial, python tutorial for beginners, python 3 tutorial, python programming, code with mosh, mosh hamedani
Id: 3Tcq_dBhIrQ
Channel Id: undefined
Length: 6min 53sec (413 seconds)
Published: Fri Nov 16 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.