[ Web Proxy ]
URL:
Viewing: https://programiz.com/python-programming/methods/list/sort [Back]  [Original]

Python List sort() (with Code Visualization)
Search tutorials & examples:

Python List sort()

The sort() method can be used to sort a list in either ascending or descending order. It also allows us to sort a list based on a function (such as len() to sort a list based on length of its items).

Here's a quick example:

numbers = [11, 3, 7, 5, 2]

# Sort in ascending order
numbers.sort()
print(f'Ascending order: {numbers}')

# Sort in descending order
numbers.sort(reverse=True)
print(f'Descending order: {numbers}')

Output

Ascending order: [2, 3, 5, 7, 11]
Descending order: [11, 7, 5, 3, 2]

sort() Syntax

The syntax of sort() is:

numbers.sort(reverse=False, key=None)

Arguments

sort() allows two optional keyword arguments:

  • reverse - If omitted, defaults to False. If True is passed, list is sorted in descending order.
  • key - If omitted, defaults to None. If a function is passed as key, items are sorted based on values returned by the function.

Return Value

sort() doesn't return any value (returns None).


Example: Sorting String Items

If a list contains strings as its items, sort() arranges them in alphabetical order.

models = ["Gemini", "ChatGPT", "Claude"]

# Sort in alphabetical order
models.sort()
print(f'Alphabetical: {models}')

# Sort in reverse alphabetical order
models.sort(reverse=True)
print(f'Reverse alphabetical: {models}')

Output

Alphabetical: ['ChatGPT', 'Claude', 'Gemini']
Reverse alphabetical: ['Gemini', 'Claude', 'ChatGPT']

Note: If a list contains both strings and numbers, sort() raises TypeError because they are not comparable.


Example: Sort Based on String's Length

As mentioned, sort() takes an optional key argument, which allows us to sort items based on a function.

For example, passing len as key sorts the items based on their length. It's because len() returns the length of a string.

models = ["Gemini", "Opus", "Kimi K3"]

# Sort by length
models.sort(key=len)
print(models)

# Sort by length in reverse order
models.sort(reverse=True, key=len)
print(models)

Output

['Opus', 'Gemini', 'Kimi K3']
['Kimi K3', 'Gemini', 'Opus']

Example: Sort Based on a Function

We can also sort items based on a custom function. In this example, we'll use a lambda function to sort a list of tuples.

models = [("Claude", 2023), ("DeepSeek", 2025), ("ChatGPT", 2022)]

# Sort based on release date
models.sort(key=lambda model: model[1])
print(models)

# Sort by latest release date
models.sort(key=lambda model: model[1], reverse=True)
print(models)

Output

[('ChatGPT', 2022), ('Claude', 2023), ('DeepSeek', 2025)]
[('DeepSeek', 2025), ('Claude', 2023), ('ChatGPT', 2022)]

Here, our function lambda model: model[1] returns the second element of a tuple, which is the release date. The sort() function sorts the list items (tuples) based on it.

Also Read:

Did you find this article helpful?
phone:
submitted[dislike_feedback]:

Web Proxy Viewer  |  New URL  |  Original Page