Lesson 11: 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 it will be explained 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.

Next Lesson:

.pop () function

Claude-Joseph Vernet, Calm


Claude Joseph Vernet

Calm

Calm by Claude Joseph Vernet

“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