How to Sort Lists in Python

Python lists are mutable data structures. This means you can do plenty of operations with them, including sorting.

In this article, we will learn how to sort Lists in Python. To follow the tutorial, you need to have:

  • Python 3 and above
  • Your preferred text editor (e.g., IDLE).

Understanding the sort() method

The sort() method is Python’s in-built method to sort a list. It has the following syntax:

chosen_list.sort(reverse = ...., key = .... )

Where:

  • chosen_ list is the list you worked with.
  • sort() is the function that handles list sorting.
  • reverse is used to tell the sort method if you want the list to be sorted in descending or ascending order. You can input True or False. If you do not provide any reverse parameter, it will default to False, which sorts the list in ascending order. To sort the function in descending order, you must provide reverse = True.
  • TheĀ Key parameter can be used to specify more detailed sorting criteria.

Sorting the list in ascending order

The sort() method sorts lists in ascending order if you do not provide any parameters. This means it will arrange the items in a list from the lowest to the highest order.

#creating a list of random numbers
numbers = [84, 32, 1, 5, 90, 23, 18, 33, 199, 100]

#using the sort() method on numbers
numbers.sort()

#print numbers
print(numbers)

#output
[1, 5, 18, 23, 32, 33, 84, 90, 100, 199]

As you can see, the numbers list gets sorted in ascending order.

Sorting the list in descending order

Next, we will use the reverse=True to sort the list in descending order. Let’s check the example below, using the same numbers from the previous example.

#creating a list of random numbers
numbers = [84, 32, 1, 5, 90, 23, 18, 33, 199, 100]

#using the sort() method on numbers
numbers.sort(reverse=True)

#print numbers
print(numbers)

#output
[199, 100, 90, 84, 33, 32, 23, 18, 5, 1]

As you can see, the numbers list gets sorted in descending order.

Sorting string list

Until now, we have sorted a list containing numbers. However, you can also use the sort() method to sort a string list.

Let’s see an example.

#a list of strings
fruits = ['mango', 'grapes', 'avacado', 'tomato', 'cocount', 'blueberry', 'cherry']

#using the sort() function
fruits.sort()

#print fruits
print(fruits)

#output
['avacado', 'blueberry', 'cherry', 'cocount', 'grapes', 'mango', 'tomato']

Here, the sorting is done based on alphabetical order [a-z].

To sort the string list in descending order, we need to specify it using the reverse parameter.

#a list of strings
fruits = ['mango', 'grapes', 'avacado', 'tomato', 'coconut', 'blueberry', 'cherry']

#using the sort() function with reverse = True
fruits.sort(reverse=True)

#print fruits
print(fruits)

#output
['tomato', 'mango', 'grapes', 'coconut', 'cherry', 'blueberry', 'avacado']

Now, the list of the fruits is sorted in reverse order [z-a].

Sorting lists using the key parameter

sort() method parameter allows you to sort a list based on customized sorting operations. To do so, assign a method or function to the key parameter. The sorting rule is applied to every item in the list when called via the key parameter.

Let’s see the example below to get a better idea.

#a list of strings
fruits = ['mango', 'grapes', 'avacado', 'tomato', 'coconut', 'blueberry', 'cherry']

#using the sort() function with key= len
fruits.sort(key=len)

#print fruits
print(fruits)

#output
['mango', 'grapes', 'tomato', 'cherry', 'avacado', 'coconut', 'blueberry']

Here, we called the len() function, which sorts the list based on the length of each item(string) in the list.

You can also call your own function in the key parameter.

def myFunc(e):
    return len(e)

#a list of strings
fruits = ['mango', 'grapes', 'avacado', 'tomato', 'cocount', 'blueberry', 'cherry']

#using our defined function 
fruits.sort(key=myFunc)

#print fruits
print(fruits)

#output
['mango', 'grapes', 'tomato', 'cherry', 'avacado', 'cocount', 'blueberry']

Sorted() method

sorted() method is a built-in function that sorts lists in ascending order by default. It has the following syntax:

sorted(list_name, reverse=...., key=...)
  • list_name parameter must be included, whereas the reverse and key parameters are optional.
  • The reverse parameter can be set as either True and False. True sorts the list in descending order, and False in ascending order.

What is the difference between the sort() and sorted() method?

The sorted() method returns a new sorted copy instead of sorting the original given list. This is important as you may not want to alter the state of the original list and want to store the result in a new list.

Let’s see an example below.

#creating a list of random numbers
numbers = [84, 32, 1, 5, 90, 23, 18, 33, 199, 100]

#using the sort() method on numbers
numbers.sort()

#using the sorted() method on numbers and sorting it in a new list
new_numbers = sorted(numbers, reverse=True)

#print numbers
print(numbers)

#print new_numbers
print(new_numbers)

#output
[1, 5, 18, 23, 32, 33, 84, 90, 100, 199]
[199, 100, 90, 84, 33, 32, 23, 18, 5, 1]

When to use sort() and sorted()

  • sort() works with all data types.
  • sort() is faster and consumes less memory. This is because it doesn’t create a new list and sorts the original list.
  • If you are using a list that you do not want to modify, useĀ sorted() as the sort() method modifies the list.
  • sorted() function provides more functionality as you can return a new list and then perform operations on it.
  • sorted() works on iterable data types including sets, tuples, and dictionaries.

Sorting lists is a critical part of any Python programming. We hope you found this guide useful and can now confidently sort a list.

Was this article helpful?

Leave a Comment