How to Python list sort()

The sort() method in Python is employed to arrange the elements of a list in ascending, descending, or custom-defined order, thereby enabling efficient organization of the data. Upon invoking the sort() method without any arguments, it iterates over the list elements in a loop, effectively rearranging them in ascending order. To sort the list in descending order, one can utilize the reverse=True argument, which instructs the method to rearrange the elements in reverse order, thereby achieving a descending sequence.

sort() method

Python provides an elegant and straightforward way to arrange the elements of a list in the desired order, catering to the specific needs of data processing and analysis. The ability to customize the sorting order empowers programmers to handle diverse scenarios and optimize the organization of data for efficient retrieval and manipulation.

list.sort(reverse=TrueFalse, key=myFunc)
  1. reverse - Default is reverse=False, while reverse=True will sort the list descending.(Optional)

  2. key - A method to specify the sorting criteria (Optional)

How to Python list sort

Sort list of numbers

python list sort alphabetically

Sort list of strings

python list sort ascending

Sort list of strings in descending order

sList = ['orange','banana','grapes','apple'] sList.sort(reverse = True) print(sList)
['orange', 'grapes', 'banana', 'apple']

If any of the list element is Uppercase , let's see what happens.

python list sort descending order

It is because Python treats all Uppercase letters to be lower than Lowercase letters, if you want you can change it.


python list sort lambda

Sort using key parameter

The key parameter in the sort() method of a list allows you to specify a custom function that will be called on each item of the list before making comparisons during the sorting process. This parameter provides a way to define your own implementation for sorting the elements based on a specific criterion or attribute.

The key parameter is optional, and if provided, it should be a function that takes an item from the list as an argument and returns a value that will be used for sorting. The sort() method will use these generated values for comparison instead of the original items in the list.

def keyArgs(args): return args[1] numList = [('Five', '5'), ('Two', '2'), ('Four', '4'), ('One', '1'), ('Three','3')] numList.sort(key=keyArgs) print(numList)
[('One', '1'), ('Two', '2'), ('Three', '3'), ('Four', '4'), ('Five', '5')]

Sort using key parameter (reverse)

def keyArgs(args): return args[1] numList = [('Five', '5'), ('Two', '2'), ('Four', '4'), ('One', '1'), ('Three','3')] numList.sort(key=keyArgs,reverse = True) print(numList)
[('Five', '5'), ('Four', '4'), ('Three', '3'), ('Two', '2'), ('One', '1')]

Sort the list by the length of the values

A new function "sortLen" that returns the length of the list elements and add this function as key of sort() function.

def sortLen(item): return len(item) items = ['aaaaa', 'aa', 'aaaa', 'aaa', 'a'] items.sort(key=sortLen) print(items)
['a', 'aa', 'aaa', 'aaaa', 'aaaaa']

Sorting a List of Tuples

A tuple is a collection of Python objects written within round brackets () and separated by commas. The key distinction between tuples and lists is that tuples are immutable, meaning their elements cannot be changed, added, or removed after the tuple is created. In contrast, lists are mutable, allowing modifications to their elements.

This immutability makes tuples suitable for situations where you need fixed data or want to ensure the integrity of the data throughout your program's execution. In contrast, lists are more appropriate when you require a data structure that can be modified, expanded, or contracted during the course of your program.

Sorting a List of Tuples by the first item

tList = [(8,12), (5,14), (7,18), (6,13), (10,19), (9,17)] tList.sort() print(tList)
[(5, 14), (6, 13), (7, 18), (8, 12), (9, 17), (10, 19)]

Sorting a List of Tuples by the second item

def secondItem(args): return args[1] tList = [(8,12), (5,14), (7,18), (6,13), (10,19), (9,17)] tList.sort(key=secondItem) print(tList)
[(8, 12), (6, 13), (5, 14), (9, 17), (7, 18), (10, 19)]

Sorting a list of objects

class Cars: def __init__(self, Name, Year): self.Name = Name self.Year = Year car1 = Cars('Ford',1903) car2 = Cars('Toyota',1937) car3 = Cars('Benz',1926) car4 = Cars('Porsche',1931) carList = [car1, car2, car3, car4] carList.sort(key=lambda x: x.Name) print([item.Name for item in carList]) carList.sort(key=lambda x: x.Year) print([item.Year for item in carList])
['Benz', 'Ford', 'Porsche', 'Toyota'] [1903, 1926, 1931, 1937]


The sort() method is used to sort the elements of a list in ascending order. It rearranges the elements in place, directly modifying the original list without creating a new one. Additionally, the sort() method allows for custom sorting by using the key parameter, which specifies a function to be applied to each element before making comparisons.