Python List sort()
The sort()
method sorts the list in place, meaning it modifies the original list. By default, it sorts in ascending order.
Syntax
list.sort(key=None, reverse=False)
Parameters
key
: (Optional) A function that defines a sorting criteria. This function is applied to each element in the list, and its return value (the key
) is used for sorting instead of the actual elements. By default, it’s None
.
reverse
: (Optional) A boolean value. If set to True
, the list is sorted in descending order. By default, it’s False
.
Return Value
The sort()
method doesn’t return anything i.e. returns None
. It doesn’t return a new list, it modifies the original list.
Example: Sort a list in ascending order
my_list = [5, 15, 20, 10]
my_list.sort()
print(my_list) # Output: [5, 10, 15, 20]
Sort a list in descending order
You can sort a list in descending order by setting the reverse
parameter of the sort()
method to True
.
For example:
my_list = [5, 15, 20, 10]
my_list.sort(reverse=True)
print(my_list) # Output: [20, 15, 10, 5]
Sort a list of strings
You can also sort a list of strings alphabetically using the sort()
method.
For example:
my_list = ["mango", "apple", "orange", "banana"]
# Sort a list alphabetically in ascending order
my_list.sort()
print(my_list) # Output: ['apple', 'banana', 'mango', 'orange']
# Sort a list alphabetically in descending order
my_list.sort(reverse=True)
print(my_list) # Output: ['orange', 'mango', 'banana', 'apple']
Sort a list using the key parameter
The key
parameter of the sort()
method allows you to define a function that determines how each element of the list should be compared during sorting.
This function is applied to each element in the list before comparisons are made, and the returned value is used for comparisons during sorting.
For example, you can sort a list of strings by their length rather than alphabetically by using key=len
, which applies len()
function to each string to determine its sorting order.
my_list = ["chicken", "cat", "horse", "goat"]
# Sort a list of strings by length
my_list.sort(key=len)
print(my_list) # Output: ['cat', 'goat', 'horse', 'chicken']
Sort with the custom function
You can define your custom function to use with the key
parameter of the sort()
method. This is useful when sorting items in a list based on a specific property or condition.
Example: Sort a list of dictionaries by a specific key
my_list = [
{"name":"James", "age": 45},
{"name":"Ethan", "age": 55},
{"name":"Tony", "age": 40}
]
# Custom function to return the age
def get_age(person):
return person["age"]
# Sort a list of dictionary by age
my_list.sort(key=get_age)
print(my_list)
# Output: [{'name': 'Tony', 'age': 40}, {'name': 'James', 'age': 45}, {'name': 'Ethan', 'age': 55}]
Alternatively, you can achieve the same result with a lambda function.
my_list = [
{"name":"James", "age": 45},
{"name":"Ethan", "age": 55},
{"name":"Tony", "age": 40}
]
# Sort a list of dictionary by age
my_list.sort(key=lambda x: x["age"])
print(my_list)
# Output: [{'name': 'Tony', 'age': 40}, {'name': 'James', 'age': 45}, {'name': 'Ethan', 'age': 55}]
If you need to sort a list without modifying the original list, you should use the sorted()
function instead of the sort()
method.