Python List Sorting with sorted() and sort()

Sometimes you need to write a code to sort list items in some situations. In this article, we will discuss multiple ways to sorting Python list. Because, Python sorting functionality offers great features to done basic sorting or customize sorting ordering at a granular level.

First of all, you have a python list and you want to sort the items. You can use sort() or sorted() method to achieve it. Because, Python drive with two built-in methods for sorting lists and other iterable items.

Python List Sorting

The difference between sort and sorted, the sort is a list method that modifies the original list in place. However, sorted is a built-in function. It is creates a new list without changing the original list. However, before we start ahead, you need to understanding python lists, and tuples.

Python sort() method

The sort() method sorts the elements of a given list in a specific order (Ascending or Descending).


The syntax of sort() method is:

list.sort(key=customFun, reverse=True|False)
Parameters Values

reverse: Optional. default is False. In contrast if True, it will sort the list descending.
key: Optional. A function to specify the sorting formula.

Python sorted() method

The sorted() method sorts any sequence, and always returns a sorted sequence without modifying the original sequence.


The syntax of sorted() method is:

sorted(iterable, key=customFun, reverse=True|False)
Parameters Values

iterable: Required. The sequence to sort (list, tuple).
reverse: Optional. default is False. If True, it will sort the list descending.
key: Optional. A function to specify the sorting formula.

Difference between sort() and sorted()

The sort() method does not return any value. But, it will change the original list. However, if you want to keep the original list sequence, use sorted() function.

Example 1: Sorting Numbers

Here, list items are sorted in descending order.

>>> numbers = [1, 5, 4, 2, 3]
>>> numbers.sort(reverse = True)
[5, 4, 3, 2, 1]

Example 2: Sorting Alphabetically

Following example sort string list item alphabetically.

>>> vowels = ['e', 'i’, 'u', 'o', 'a']
>>> vowels.sort()
['a', 'e', 'i', 'o', 'u']

Example 3: Understand sort() and sorting()

The sorted() can be used on a list, and the output is assigned to a variable.

>>> numbers = [1, 5, 4, 2, 3]
>>> sorted_numbers = numbers.sort()
>>> numbers
[1, 5, 4, 2, 3]
>>> sorted_numbers
[1, 2, 3, 4, 5]

Example 4: Sort the tuple of list items (Sort by key)

First of all, you have list of tuple items that you have to sort it. Here, key parameters specify a custom function that will be called every list item before comparisons. Also, custom function returns the first item of the tuple.

# Sort by tuple first item
def myFunction(elem):
    return elem[0]

# list of tuple
list = [(4, 2), (2, 3), (1, 7), (3, 5)]

# sort list using key

# print list
[(1, 7), (2, 3), (3, 5), (4, 2)]

Example 5: Sort the tuple of list items (lambda function)

Similarly, we sort the tuple of list items using lambda function.

>>> list2 = [(4, 2), (2, 3), (1, 7), (3, 5)]
>>> list2.sort(key=lambda x: x[0])
>>> list2
[(1, 7), (2, 3), (3, 5), (4, 2)]

You can refer here official Python sort() and sorting() documentation.

We hope you have found this article helpful. Let us know your questions or feedback if any through the comment section in below. You can subscribe our newsletter and get notified when we publish new articles. Moreover, you can explore here other Python related articles.

Icon credit: Eezy

Join the Discussion.