Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Sort list of tuples by specific ordering in Python
When you need to sort a list of tuples by a specific element or custom ordering, Python's sorted() function with a key parameter provides a flexible solution. The key parameter accepts a function that determines the sorting criteria.
A list of tuples is a common data structure where each tuple contains related elements. Sorting such lists by specific tuple elements is frequently needed in data processing tasks.
Sorting by Second Element
Use a lambda function to specify which tuple element to sort by ?
def tuple_sort(my_tup):
return sorted(my_tup, key=lambda x: x[1])
my_tuple = [('Mahe', 11), ('Aisha', 33), ('Will', 50), ('Root', 65)]
print("The original list of tuples:")
print(my_tuple)
print("Sorted by second element:")
print(tuple_sort(my_tuple))
The original list of tuples:
[('Mahe', 11), ('Aisha', 33), ('Will', 50), ('Root', 65)]
Sorted by second element:
[('Mahe', 11), ('Aisha', 33), ('Will', 50), ('Root', 65)]
Sorting by First Element
Sort alphabetically by the first element of each tuple ?
students = [('John', 85), ('Alice', 92), ('Bob', 78), ('Diana', 95)]
# Sort by name (first element)
sorted_by_name = sorted(students, key=lambda x: x[0])
print("Sorted by name:")
print(sorted_by_name)
Sorted by name:
[('Alice', 92), ('Bob', 78), ('Diana', 95), ('John', 85)]
Reverse Sorting
Add reverse=True for descending order ?
students = [('John', 85), ('Alice', 92), ('Bob', 78), ('Diana', 95)]
# Sort by score in descending order
sorted_by_score_desc = sorted(students, key=lambda x: x[1], reverse=True)
print("Sorted by score (highest first):")
print(sorted_by_score_desc)
Sorted by score (highest first):
[('Diana', 95), ('Alice', 92), ('John', 85), ('Bob', 78)]
Multiple Criteria Sorting
Sort by multiple elements using a tuple as the key ?
employees = [('Sales', 'John', 50000), ('IT', 'Alice', 75000),
('Sales', 'Bob', 55000), ('IT', 'Carol', 70000)]
# Sort by department first, then by salary
sorted_employees = sorted(employees, key=lambda x: (x[0], x[2]))
print("Sorted by department, then salary:")
for emp in sorted_employees:
print(emp)
Sorted by department, then salary:
('IT', 'Alice', 75000)
('IT', 'Carol', 70000)
('Sales', 'John', 50000)
('Sales', 'Bob', 55000)
Using operator.itemgetter()
For better performance with large datasets, use operator.itemgetter() ?
from operator import itemgetter
data = [('Python', 3.9), ('Java', 8), ('C++', 17), ('JavaScript', 2021)]
# Sort by second element using itemgetter
sorted_data = sorted(data, key=itemgetter(1))
print("Sorted using itemgetter:")
print(sorted_data)
Sorted using itemgetter:
[('Python', 3.9), ('Java', 8), ('C++', 17), ('JavaScript', 2021)]
Comparison
| Method | Use Case | Performance |
|---|---|---|
lambda x: x[0] |
Single element sorting | Good |
lambda x: (x[0], x[1]) |
Multiple criteria | Good |
itemgetter(1) |
Large datasets | Faster |
Conclusion
Use sorted() with lambda functions for flexible tuple sorting. For better performance with large datasets, consider operator.itemgetter(). The reverse parameter controls ascending or descending order.
