Sort List of Lists in Python
In Python, you can sort a nested list (list of lists) based on specific criteria using the sorted()
function or the sort()
method. Here’s how you can do it:
(1) Sort by the first element in each sublist
To sort a nested list by the first element in each sublist, you can use the sorted()
function or the sort()
method.
The sorted()
function creates a new sorted list without modifying the original list.
The sort()
method sorts the elements of a list in place, meaning it modifies the original list.
For example:
my_list = [[4, 2], [1, 5], [8, 9]]
# Sorting using sorted() function
sorted_list = sorted(my_list)
print(sorted_list) # Output: [[1, 5], [4, 2], [8, 9]]
# Sorting using sort() method
my_list.sort()
print(my_list) # Output: [[1, 5], [4, 2], [8, 9]]
(2) Sort by a specific index in each sublist
To sort a nested list by a specific index, you can use the key
parameter of the sorted()
function or the sort()
method.
For example:
my_list = [[4, 9], [1, 5], [8, 3]]
# Sort by the second element using sorted()
sorted_list = sorted(my_list, key=lambda x:x[1])
print(sorted_list) # Output: [[8, 3], [1, 5], [4, 9]]
# Sort by the second element using sort()
my_list.sort(key=lambda x:x[1])
print(my_list) # Output: [[8, 3], [1, 5], [4, 9]]
You can use the itemgetter()
function from the operator
module instead of a lambda
function to extract the key, as it is more efficient.
For example:
from operator import itemgetter
my_list = [[4, 9], [1, 5], [8, 3]]
# Sort by the second element using sorted()
sorted_list = sorted(my_list, key=itemgetter(1))
print(sorted_list) # Output: [[8, 3], [1, 5], [4, 9]]
# Sort by the second element using sort()
my_list.sort(key=itemgetter(1))
print(my_list) # Output: [[8, 3], [1, 5], [4, 9]]
(3) Sort in descending order
To sort a nested list in descending order, you can set the reverse
parameter of the sorted()
function or the sort()
method to True
.
For example:
from operator import itemgetter
my_list = [[4, 9], [1, 5], [8, 3]]
# Sort by the second element in descending order using sorted()
sorted_list = sorted(my_list, key=itemgetter(1), reverse=True)
print(sorted_list) # Output: [[4, 9], [1, 5], [8, 3]]
# Sort by the second element in descending order using using sort()
my_list.sort(key=itemgetter(1), reverse=True)
print(my_list) # Output: [[4, 9], [1, 5], [8, 3]]
(4) Sort by multiple criteria
To sort a nested list by multiple criteria, you can use sorted()
or sort()
with a tuple as the key. The sorting will be done based on the tuple elements in order.
For example, let’s sort a nested list by the first element and then by the last element:
my_list = [[4, 0, 8], [1, 8, 9], [4, 5, 3]]
# Sort by the first element and then by the third element using sorted()
sorted_list = sorted(my_list, key=lambda x: (x[0], x[2]))
print(sorted_list) # Output: [[1, 8, 9], [4, 5, 3], [4, 0, 8]]
# Sort by the first element and then by the third element using sort()
my_list.sort(key=lambda x: (x[0], x[2]))
print(my_list) # Output: [[1, 8, 9], [4, 5, 3], [4, 0, 8]]
Choosing the right method
Use sorted()
if you need the original list to remain unchanged.
Use sort()
if you need to modify the list in place. It is more efficient because it avoids creating a copy of the list.