Lesson 15: Python sorted() function

It is quite straightforward to apply a simple sorted() function however, in this lesson we will check out more advanced applications with key and reverse parameters.

Although not introduced in .sort() method’s lesson for learning reasons, key and reverse parameters also work in .sort() method.

In general though sorted() function is a generally more accepted practice since it doesn’t automatically change the original list and for this reason more prone to unintentional data accidents.

For the simple use, just type your list inside sorted() function and your list will be sorted.

Unlike .sort() method sorted()function will not change your list’s order. Instead it will create a new sequence.

You can assign the result of the sorted() function to a new variable or to the existing list if you’d like to replace it.

Let’s see an example.

Function 1: sorted()

sorted() method is very useful builtin Python function. It will sort the objects inside a list. It is also very similar (almost identical) to .sort() method of Python lists.

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) inside sorted() function's parenthesis type your iterable variable's name. sorted function also takes other arguments such as:

  • "key=", for sorting based on a function
  • "reverse=" for sorting in reverse order ("reverse=False" by default)
So, correct syntax would be : sorted(list) or with optional parameters: sorted(list, key=f, reverse=True) where f is a function.

Syntax don't(s)

1) If you're going to use the key argument don't confuse the equal sign with colon. Incorrect > key:function Correct > key=function

Example 1

>>> money_earned = [550, 750, 400, 1200]
>>> money_sorted = sorted(money_earned)
>>> print(money_sorted)
>>> print(

[400, 550, 750, 1200]
[550, 750, 400, 1200]

Note that the actual list itself (money_earned) remains unchanged unlike .sort() method.

Also we pass the list inside sorted() function unlike .sort() method.

These are the only two differences between two approaches.

Let’s see another example this time with strings and text instead of numbers.

Example 2

>>> yard_animals = [“goose”, “porcupine”, “ground hog”]
>>> ya_sorted = sorted(yard_animals)
>>> print(ya_sorted)

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


1- You can also sort your list in a reverse order with using reverse parameter as: sorted(list, reverse=True)

Let’s see some examples:

Let’s do the same example with reverse parameter:

Example 3: Reverse

>>> yard_animals = [“goose”, “porcupine”, “ground hog”]
>>> ya_sorted = sorted(yard_animals, reverse=True)
>>> print(ya_sorted)

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

Let’s do the same example with reverse parameter:

Example 4: Reverse 2

>>> next_trips = [“Japan”, “Belarus”, “Tanzania”]
>>> next_trips = sorted(next trips, reverse=True)
>>> print(next_trips)

[“Tanzania”, “Japan”, “Belarus”]

This time we assigned the result directly to the original list, meaning we changed the original list intentionally.

Example 4: Reverse numbers

>>> pts = [22, 4, 66, 101, -1]
>>> pts = sorted(pts, reverse=True)
>>> print(pts)

[101, 66, 22, 4, -1]

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.

Example 4: sorted with Lambda

>>> pts = [22, 4, 66, 101, -1]
>>> pts = sorted(pts, reverse=True)
>>> print(pts)

[101, 66, 22, 4, -1]

Lesson 16

List Comprehension