Python - Sorting a dictionary of tuples
Last Updated :
23 Jul, 2025
This task becomes particularly useful when working with structured data, where tuples represent grouped information (e.g., names and scores, items and prices). Sorting such data enhances its readability and usability for further analysis.
For example, consider a dictionary d = {'student3': ('bhanu', 10), 'student2': ('uma', 12),'student4': ('sai', 11), 'student1': ('suma', 11)}, When sorted by keys, the dictionary will be {'student1': ('suma', 11), 'student2': ('uma', 12), 'student3': ('bhanu', 10), 'student4': ('sai', 11)}
. When sorted by the name, the output will be {'student3': ('bhanu', 10), 'student4': ('sai', 11), 'student1': ('suma', 11), 'student2': ('uma', 12)}
. When sorted by the score, the dictionary will be {'student3': ('bhanu', 10), 'student4': ('sai', 11), 'student1': ('suma', 11), 'student2': ('uma', 12)}
.
Using OrderedDict
OrderedDict from the collections module allows us to sort a dictionary while preserving its insertion order. It’s useful when we need the result as an ordered dictionary.
Python
from collections import OrderedDict
d = {'student3': ('bhanu', 10), 'student2': ('uma', 12),
'student4': ('sai', 11), 'student1': ('suma', 11)}
# Sort the dictionary values
print(OrderedDict(sorted(d.values())))
# Sort the dictionary items
print(OrderedDict(sorted(d.items())))
OutputOrderedDict({'bhanu': 10, 'sai': 11, 'suma': 11, 'uma': 12})
OrderedDict({'student1': ('suma', 11), 'student2': ('uma', 12), 'student3': ('bhanu', 10), 'student4': ('sai', 11)})
Explanation:
- sorted(d.values()) sorts the dictionary d values lexicographically.
- sorted(d.items()) sorts the dictionary items, key-value pairs based on the keys.
Using sorted()
This method converts the result of the sorted() function into a standard dictionary. It is straightforward and suitable for small datasets where efficiency is less critical.
Python
d = {'student1': ('bhanu', 10), 'student4': ('uma', 12),
'student3': ('suma', 11), 'student2': ('ravi', 11),
'student5': ('gayatri', 9)}
# Sort `d` based on items
for i in sorted(d.items()):
print(i, end=" ")
# Sort `d` based on values
for i in sorted(d.values(), key=lambda x: x[1]):
print(i, end=" ")
# Sort `d` based on keys
for i in sorted(d.keys()):
print(i, end=" ")
Output('student1', ('bhanu', 10)) ('student2', ('ravi', 11)) ('student3', ('suma', 11)) ('student4', ('uma', 12)) ('student5', ('gayatri', 9)) ('gayatri', 9) ('bhanu', 10) ('suma', 11) ('ravi', 11) ('uma', ...
Explanation:
- sorted(d.items()) sorts the key-value pairs alphabetically by the key .
- sorted(d.values()) sorts only the values by the second element .
- sorted(d.keys()) sorts only the values by the second element .
Using heapq
When working with dictionaries of tuples, sorting by tuple elements using heapq is particularly advantageous for large datasets, as it reduces computational overhead compared to full in-memory sorting.
Python
import heapq
d = {'student3': ('bhanu', 10), 'student2': ('uma', 12),
'student4': ('sai', 11), 'student1': ('suma', 11)}
# Sort by values (smallest to largest)
res = heapq.nsmallest(len(d), d.items(), key=lambda x: x[1])
print(res)
# Sort by values (largest to smallest)
res = heapq.nlargest(len(d), d.items(), key=lambda x: x[1])
print(res)
Output[('student3', ('bhanu', 10)), ('student4', ('sai', 11)), ('student1', ('suma', 11)), ('student2', ('uma', 12))]
[('student2', ('uma', 12)), ('student1', ('suma', 11)), ('student4', ('sai', 11)), ('stu...
Explanation:
- heapq.nsmallest retrieves all elements sorted by the tuple values (x[1]).
- heapq.nlargest retrieves all elements in descending order based on values.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice