# Lesson 15: Python sorted() function

- ESTIMATED TIME: 9 mins
- DIFFICULTY: Intermediate
- FUNCTIONS: sorted()
- 8 EXERCISES

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?

#### 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)

`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(money_earned)

[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”]

## Tips

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]

Los Cabo by Einerssen Jonn