Sorting a List of Lists in Python
Learn how to sort lists of lists efficiently using Python’s built-in sorting capabilities, with practical examples and code snippets. …
Updated June 22, 2023
Learn how to sort lists of lists efficiently using Python’s built-in sorting capabilities, with practical examples and code snippets.
What is Sorting a List of Lists?
Sorting a list of lists in Python refers to the process of arranging multiple lists (or sublists) within a parent list in a specific order. This can be based on various criteria, such as alphabetical or numerical order, or even custom sorting rules defined by you.
Imagine having a collection of student grades where each student has multiple subjects with scores:
student_grades = [
["John", "Maths", 85],
["Alice", "Science", 90],
["Bob", "English", 78],
]
You might want to sort these students by their overall average score in descending order. This is a classic use case for sorting a list of lists.
Step-by-Step Explanation
Here’s a simplified step-by-step guide to understanding how to sort a list of lists:
1. Define Your List
First, ensure your list contains sublists with elements you want to sort. For example:
fruits = [
["Apple", 5],
["Banana", 3],
["Cherry", 4]
]
2. Choose a Sorting Criterion
Decide on the basis of which you want to sort your sublists. It could be based on specific elements within each sublist or even a custom key function.
3. Apply the Sort Function
Use Python’s built-in sort()
method or the more efficient sorted()
function (which returns a new sorted list and leaves the original unchanged) along with a lambda function to specify how you want your lists to be sorted. For example, to sort fruits by their quantity in descending order:
fruits.sort(key=lambda x: x[1], reverse=True)
In this case, x[1]
refers to the second element within each sublist (the quantity of fruit), and reverse=True
sorts the list in descending order.
4. Print or Return Your Sorted List
Finally, you can print out or return your sorted list:
print(fruits)
Advanced Sorting Scenarios
- Sorting based on multiple criteria: You can sort lists by multiple elements within each sublist using a custom key function.
students = [
["John", "Maths", 85],
["Alice", "Science", 90],
["Bob", "English", 78]
]
def sort_students(students):
return sorted(students, key=lambda x: (x[1], x[2]))
sorted_students = sort_students(students)
print(sorted_students)
In this example, students are first sorted by their subject in ascending order (key=lambda x: x[1]
), and then by their score within each subject (key=lambda x: x[2]
).
Conclusion
Sorting a list of lists in Python provides flexibility to manage multidimensional data structures efficiently. This tutorial has covered the basics of sorting along with more advanced scenarios, including custom key functions for sorting based on multiple criteria.
By understanding how and when to apply the sort function or sorted function, you can make informed decisions about your code’s performance and readability. Practice these concepts through interactive coding exercises and real-world projects to solidify your skills in Python programming.