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.

Used Where?

When you have data with mixed values, sometimes it can be very useful to quickly sort it and see it that way. You might be curious about the relation between elements, increments or min and max values etc.

Syntax do(s)

1) .sort() is a method and it's correct application is calling it on a list such as list.sort()

Syntax don't(s)

1) .sort() method will change your list's order. When you call .sort() method on a list your original list will be changed so if you'd like to preserve the original order either make a copy of your list or use another sorting function that won't change your data (see: sorted() function).

Example 1

>>> money_earned = [550, 750, 400, 1200]
>>> money_earned.sort()
>>> print(money_earned)

[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.

Example 2

>>> yard_animals = [“goose”, “porcupine”, “ground hog”]
>>> yard_animals.sort()
>>> print(yard_animals)

[“goose”, “ground hog”, “porcupine”]

Tips

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.

Example 4

>>> next_trips = [“Japan”, “Belarus”, “Tanzania”]
>>> next_trips.sort(reverse=True)
>>> print(next_trips)

[“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.).

“Have you installed Python yet?”
Having Python setup on your computer can expedite the learning experience and help you gain the real life skills in a non-simulation environment. Installing Python can have different nuances that can be confusing for everyone. We compiled a complete tutorial about the best and most convenient Python installing practices Check it out by clicking the button below.
SANDRA FILES