How to Sort a List in Python (examples included)

You may use the following approach in order to sort a list in Python in an ascending order:

yourlist.sort()

Alternatively, you may use this syntax to sort a list in a descending order:

yourlist.sort(reverse = True)

Next, you’ll see 4 cases of sorting a:

  1. List in an ascending order
  2. List in a descending order
  3. List of Lists
  4. List of Lists for a specific index

Case 1: Sort a List in Python in an Ascending Order

Let’s say that you created a simple list of names:

names = ['Jon','Maria','Bill','Liam','Emma']
print (names)

This is how the list would look like in Python:

['Jon', 'Maria', 'Bill', 'Liam', 'Emma']

You can then sort the list of names in an ascending order by adding names.sort() to the code:

names = ['Jon','Maria','Bill','Liam','Emma']
names.sort()
print (names)

You’ll see that the list is now sorted in an ascending order, where Bill is the first name to be displayed, while Maria would be the last:

['Bill', 'Emma', 'Jon', 'Liam', 'Maria']

Case 2: Sort a List in Python in a Descending Order

Alternatively, you could sort the list in a descending order by setting reverse = True as captured below:

names = ['Jon','Maria','Bill','Liam','Emma']
names.sort(reverse = True)
print (names)

Now Maria would be the first name, while Bill would be the last:

['Maria', 'Liam', 'Jon', 'Emma', 'Bill']

Case 3: Sort a List of Lists

What if you have a list of lists?

For example, let’s say that you have the following dataset:

Name Country Age
Jon Brazil 21
Maria Iceland 38
Bill Spain 42
Liam Japan 28
Emma Italy 51

You can capture the above dataset in Python using a list of lists:

people_list = [['Jon','Brazil',21],['Maria','Iceland',38],['Bill','Spain',42],['Liam','Japan',28],['Emma','Italy',51]]
print (people_list)

This is how the list of lists would look like in Python:

[['Jon', 'Brazil', 21], ['Maria', 'Iceland', 38], ['Bill', 'Spain', 42], ['Liam', 'Japan', 28], ['Emma', 'Italy', 51]]

You may then apply the following code to sort the list of lists in an ascending order:

people_list = [['Jon','Brazil',21],['Maria','Iceland',38],['Bill','Spain',42],['Liam','Japan',28],['Emma','Italy',51]]
people_list.sort()
print (people_list)

As you can see, the list of lists will now get sorted based on the first name:

[['Bill', 'Spain', 42], ['Emma', 'Italy', 51], ['Jon', 'Brazil', 21], ['Liam', 'Japan', 28], ['Maria', 'Iceland', 38]]

But what if you want to sort the list of lists on a different column?

In the final case below, you’ll see how to accomplish this goal.

Case 4: Sort a List of Lists for a Specific Column/Index

Suppose that you want to sort the list of lists based on the Age column (which has the index of ‘2’).

In that case, you may use lambda to sort the list of lists for a specific index:

people_list = [['Jon','Brazil',21],['Maria','Iceland',38],['Bill','Spain',42],['Liam','Japan',28],['Emma','Italy',51]]
people_list.sort(key = lambda i: i[2])
print (people_list)

Run the code, and you’ll see that indeed the list of lists is sorted based on the Age:

[['Jon', 'Brazil', 21], ['Liam', 'Japan', 28], ['Maria', 'Iceland', 38], ['Bill', 'Spain', 42], ['Emma', 'Italy', 51]]

You may also want to check the following guide that explains how to sort Pandas DataFrame.