Let’s check out some exercises that will help understand sorted() function better.

Exercise 15-a

Using sorted() function, sort the list in ascending order.


sorted() function sorts in ascending order by default.

lst2 = sorted(lst1)

Exercise 15-b

Using sorted() function, sort the list from a to z.


sorted() function sorts in ascending order (also a to z in strings) by default.

lst2 = sorted(lst1)

Exercise 15-c

Using sorted() function sort the list from z to a.


sorted() function sorts in ascending order (also a to z in strings) by default.

sorted() function sorts in ascending order (also a to z in strings) by default. If you’d like to reverse the sorting order you can simply set reverse parameter to True:

sorted(list, reverse=True)

lst2 = sorted(lst1, reverse = True)

Exercise 15-d

Using sorted() function sort the list in descending order.


sorted() function sorts in ascending order (also a to z in strings) by default.

sorted() function sorts in ascending order (also a to z in strings) by default. If you’d like to reverse the sorting order you can simply set reverse parameter to True:

sorted(list, reverse=True)

lst2 = sorted(lst1, reverse = True)

Exercise 15-e

Using len function and sorted() function, sort the list based on the length of the strings (In ascending order).


You can use “key=” parameter inside the sorted function to do the sorting task based on another function.

Builtin len function can be passed to key parameter. It will tell the sorted() function lengths of each item and sorted() function will sort based on these values.

sorted(list, key=len)

lakes2 = sorted(lakes1, key = len)

Exercise 15-f

Using len function and sorted() function, sort the list based on the length of the strings this time in descending order.


You can use “key=” parameter inside the sorted function to do the sorting task based on another function.

Builtin len function can be passed to key parameter. It will tell the sorted() function lengths of each item and sorted() function will sort based on these values.

sorted(list, key=len)

You can use “reverse=” parameter to reverse the sorting order.

lakes2 = sorted(lakes1, key = len, reverse = True)

Exercise 15-g

Using lambda and sorted() function, sort the list based on last characters of the items from z to a.


You can use “key=” parameter inside the sorted function to do the sorting task based on another function.

You can build your own lambda function and pass it to “key=” parameter.

sorted(list, key=lambda x: x…)

You can use “reverse=” parameter to reverse the sorting order.

lakes2 = sorted(lakes1, key = lambda x: x[-1], reverse = True)

Exercise 15-h

Using lambda and sorted() function, sort the list based on the remainder from dividing each element to 5 (From greater to smaller).


x%5 will return the remainder from dividing x to 5.

You can build your own lambda function and pass it to “key=” parameter.

sorted(list, key=lambda x: x…)

You can use “reverse=” parameter to reverse the sorting order.

lst2 = sorted(lst1, key= lambda x: x%5, reverse = True)