Different ways of sorting Python Dictionary by Keys

Sorting refers to the technique of arranging elements in a defined order. Python provides several methods to sort dictionary keys. This article covers the most common approaches for sorting dictionaries by their keys.

Using sorted() Function

The sorted() function sorts elements in iterable data structures and returns a new sorted list. For dictionaries, it can sort the keys alphabetically ?

data = {"Name": ["John", "Alice", "Bob"], 
        "Age": [25, 30, 35], 
        "City": ["NYC", "LA", "Chicago"]}

print("Original dictionary:", data)
sorted_keys = sorted(data.keys())
print("Sorted keys:", sorted_keys)
Original dictionary: {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35], 'City': ['NYC', 'LA', 'Chicago']}
Sorted keys: ['Age', 'City', 'Name']

Creating New Dictionary with Sorted Keys

To create a new dictionary with keys in sorted order, use dictionary comprehension ?

data = {"Name": ["John", "Alice", "Bob"], 
        "Age": [25, 30, 35], 
        "City": ["NYC", "LA", "Chicago"]}

print("Original dictionary:", data)
sorted_dict = {key: data[key] for key in sorted(data.keys())}
print("Sorted dictionary:", sorted_dict)
Original dictionary: {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35], 'City': ['NYC', 'LA', 'Chicago']}
Sorted dictionary: {'Age': [25, 30, 35], 'City': ['NYC', 'LA', 'Chicago'], 'Name': ['John', 'Alice', 'Bob']}

Using collections.OrderedDict()

The OrderedDict class maintains insertion order and can create sorted dictionaries ?

import collections

data = {"Name": ["John", "Alice", "Bob"], 
        "Age": [25, 30, 35], 
        "City": ["NYC", "LA", "Chicago"]}

print("Original dictionary:", data)
sorted_dict = collections.OrderedDict(sorted(data.items()))
print("Sorted OrderedDict:", sorted_dict)
Original dictionary: {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 30, 35], 'City': ['NYC', 'LA', 'Chicago']}
Sorted OrderedDict: OrderedDict([('Age', [25, 30, 35]), ('City', ['NYC', 'LA', 'Chicago']), ('Name', ['John', 'Alice', 'Bob'])])

Custom Sorting with Key Parameter

Use the key parameter for custom sorting, such as by key length ?

data = {"Name": ["John", "Alice"], 
        "A": [25, 30], 
        "City": ["NYC", "LA"]}

print("Original dictionary:", data)

# Sort by key length
sorted_by_length = dict(sorted(data.items(), key=lambda x: len(x[0])))
print("Sorted by key length:", sorted_by_length)

# Sort keys in reverse order
sorted_reverse = dict(sorted(data.items(), reverse=True))
print("Sorted in reverse:", sorted_reverse)
Original dictionary: {'Name': ['John', 'Alice'], 'A': [25, 30], 'City': ['NYC', 'LA']}
Sorted by key length: {'A': [25, 30], 'Name': ['John', 'Alice'], 'City': ['NYC', 'LA']}
Sorted in reverse: {'Name': ['John', 'Alice'], 'City': ['NYC', 'LA'], 'A': [25, 30]}

Comparison

Method Returns Best For
sorted(dict.keys()) List of keys Getting sorted key list
Dictionary comprehension New dictionary Modern Python (3.7+)
OrderedDict OrderedDict object Older Python versions
Custom key function Customized sorting Complex sorting logic

Conclusion

Use dictionary comprehension with sorted() for simple key sorting in modern Python. For custom sorting requirements, use the key parameter with lambda functions or custom functions.

Updated on: 2026-03-27T15:52:40+05:30

247 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements