In this tutorial, you will learn how to sort a list in Python, by following the below three steps:
- Options to sort a list in Python
- What is the difference between “sort” and “sorted”
- An example of using “sort”
- An example of using “sorted”
- How to Sort a List in Reverse
- Sort a list in Reverse using “sort”
- Sort a list in Reverse using “sorted”
- Sort with a Custom Function using Key
1. Options to sort a list in Python
There exist two main options, those are sort()
and sorted()
, let’s explore them below.
What is the difference between “sort” and “sorted”
list.sort()
changes the list in place and returns None
.
sorted()
takes an iterable and returns a new list, sorted.
An example of using “sort”
# create a list of unordered items our_list = ["f", "a", "c", "z", "b"] # sort the list new_list = our_list.sort() # print the new list print(new_list) # result: None print(our_list) # result: ['a', 'b', 'c', 'f', 'z']
Notice how it changes the list itself, but does not return a value to be placed into a new variable.
An example of using “sorted”
# create a list of unordered items our_list = ["f", "a", "c", "z", "b"] # sort the list new_list = sorted(our_list) # print the new list print(new_list) # result: ['a', 'b', 'c', 'f', 'z']
Notice how the changes are saved to the new list/variable.
2. How to Sort a List in Reverse
Using the above functions, it’s simple to sort a list in reverse. Just specify the reverse
argument and set it to True
.
Sort a List in Reverse using “sort”
# create a list of unordered items our_list = ["f", "a", "c", "z", "b"] # sort the list new_list = our_list.sort(reverse=True) print(our_list) # result: ['z', 'f', 'c', 'b', 'a']
Sort a List in Reverse using “sorted”
# create a list of unordered items our_list = ["f", "a", "c", "z", "b"] # sort the list new_list = sorted(our_list, reverse=True) # print the new list print(new_list) # result: ['z', 'f', 'c', 'b', 'a']
3. Sort with a Custom Function using Key
It is also possible to implement your own sorting method. This is done by adding a key
parameter and setting it to a method.
For example:
# create a list of different length items our_list = ["fasd", "asstt", "cqasf", "zqfwsef", "ba"] # sort the list, by `len` new_list = sorted(our_list, key=len) # print the new list print(new_list) # result: ['ba', 'fasd', 'asstt', 'cqasf', 'zqfwsef']
Just as we used sorted
here, you could also have used list.sort
as follows:
our_list.sort(key=len)
As before, you can also combine the reverse=True
argument should you want the reverse order.
our_list.sort(key=len, reverse=True) # or sorted(our_list, key=len, reverse=True)
You could also swap out the use of the len
method as the custom key
with your own function, or lambda, to sort by some custom choice.
For example:
student_tuples = [('john', 'A', 15),('jane', 'B', 12),('dave', 'B', 10),] sorted(student_tuples, key=lambda student: student[2]) # result: [('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Or in another example:
lst = [('candy','30','100'), ('apple','10','200'), ('baby','20','300')] lst.sort(key=lambda x:x[1]) print(lst) # result: [('apple', '10', '200'), ('baby', '20', '300'), ('candy', '30', '100')]