Lesson 11: Python sort() method
It is quite straightforward to apply a simple .sort() method however there can also be more advanced applications which we will see in the upcoming levels.
Normally, just type your lists variable name and add .sort() to it and your list will be sorted. Let’s see an example.
Function 1: .sort()
.sort() method is very useful and practical list method. It will sort the objects inside a list.
>>> money_earned = [550, 750, 400, 1200]
[400, 550, 750, 1200]
Note that the actual list itself changed and became a sorted version of itself. In later lessons we will see a useful function that can overcome situation to avoid confusion named: sorted()
Let’s see another example this time with strings and text instead of numbers.
>>> yard_animals = [“goose”, “porcupine”, “ground hog”]
[“goose”, “ground hog”, “porcupine”]
Python's Intelligent Sort Method Explained by Rune
1- You can also sort your list in a reverse order. For this, you can add a parameter inside your sort method parenthesis as: .sort(reverse=True)
Methods and functions can take single or multiple parameters. When they take multiple parameters these are separated with commas. We will see these examples in the upcoming lessons.
Let’s see an example with a list being sorted in reverse order by using .sort() method.
>>> next_trips = [“Japan”, “Belarus”, “Tanzania”]
[“Tanzania”, “Japan”, “Belarus”]
In this example, you can see that the list is reverse sorted based on the first alphabetic orders. If two strings start with the same letter then program will look at the second letters and so on.
Advanced Concepts (Optional)
1- .sort() method can also specify sorting criteria using a parameter called: “key“.
As this concept is slightly more complex we will make more examples with it in the intermediate lessons with the sorted() function.
Besides that, .sort() method and sorted() function work in the same way except .sort() changes the actual list and sorted() doesn’t.
Example 5: Sort method with based on a function
Sort method and sorted function (we will see later) can accomplish very similar things. They both also have a parameter called key which can be used to sort values based on a function.
In sorted function’s lesson, you can see many more examples with lambda functions as well as user-defined functions and these can be applied to sort method as well using key parameter.
Let’s see a simple example. Let’s say we have a nested list which consists of multiple lists and we would like to sort this list based on sum of inner lists’ values.
All you have to do this pass the criteria function to the key parameter as following.
lst = [[500, 1], [5, 55], [100,100], [500,2]] lst.sort(key=sum) print(lst)
[[5, 55], [100, 100], [500, 1], [500, 2]]
sort method is in ascending order by default. You can easily reverse this sorting algorithm by adding reverse=True parameter.
Nested Lists and Nested Dictionaries are very common data structures in Python. You can encounter them in database queries, Rest API connections, Json format etc. So it’s highly practical and used professionally so it makes sense to get familiar with nested data types and its different variations (lists in dictionary, dictionary in list, lists in list etc.).