How to Sort a Python Dictionary by Key Alphabetically?

Recently in a Python webinar topic of discussion was sorting dictionaries by key alphabetically. After exploring more about this topic I found a few effective methods to accomplish tasks. In this tutorial, I will explain how to sort a Python dictionary by key alphabetically with suitable examples and screenshots.

Sort a Python Dictionary by Key Alphabetically

To sort a Python dictionary by key alphabetically, consider you have a dictionary of U.S. state capitals like this:

state_capitals = {
    'California': 'Sacramento',
    'New York': 'Albany',
    'Texas': 'Austin',
    'Florida': 'Tallahassee',
    'Illinois': 'Springfield'
}

When you print this dictionary, the order of the key-value pairs may not be alphabetical by state name. That’s because dictionaries in Python are unordered by default. To make the dictionary more readable and organized, we can sort it alphabetically by the state names (keys).

Read How to Reverse a Dictionary in Python?

Solution 1: Use the sorted() Function

The simplest way to sort a Python dictionary by its keys alphabetically is by using the built-in sorted() function along with the items() method of the dictionary:

sorted_capitals = dict(sorted(state_capitals.items(), key=lambda x: x[0]))
print(sorted_capitals)

Output:

{'California': 'Sacramento', 'Florida': 'Tallahassee', 'Illinois': 'Springfield', 'New York': 'Albany', 'Texas': 'Austin'}

I executed the above example code and added the screenshot below.

Sort a Python Dictionary by Key Alphabetically

The items() method returns a list of key-value pairs as tuples. We pass this list to sorted(), which sorts the list based on a key function. Here, we use a lambda function x: x[0] to specify that we want to sort based on the first element of each tuple (the dictionary key).

Check out How to Save Python Dictionary to a CSV File?

Solution 2: Use the dict() and sorted() Functions

Another way to sort a Python dictionary by key is by using the dict() constructor along with the sorted() function:

sorted_capitals = dict(sorted(state_capitals.items()))
print(sorted_capitals)

Output:

{'California': 'Sacramento', 'Florida': 'Tallahassee', 'Illinois': 'Springfield', 'New York': 'Albany', 'Texas': 'Austin'}

I executed the above example code and added the screenshot below.

How to Sort a Python Dictionary by Key Alphabetically

This gives the same output as Solution 1. The sorted() function sorts the key-value pairs based on the keys by default.

Read How to Write a Dictionary to a File in Python?

Sort Dictionaries with Custom Keys

What if the dictionary keys are not simple strings, but more complex objects? We can still sort the dictionary by providing a custom key function to sorted() in Python.

For example, let’s say we have a dictionary of U.S. presidents with their names and ages:

presidents = {
    'John F. Kennedy': 43,
    'Barack Obama': 47,
    'George Washington': 57,
    'Abraham Lincoln': 52
}

To sort this dictionary by the president’s name alphabetically, we can use a lambda function that converts the name to lowercase:

sorted_presidents = dict(sorted(presidents.items(), key=lambda x: x[0].lower()))
print(sorted_presidents)

Output:

{'Abraham Lincoln': 52, 'Barack Obama': 47, 'George Washington': 57, 'John F. Kennedy': 43}

I executed the above example code and added the screenshot below.

Sort a Python Dictionary by Key Alphabetically custom keys

Check out How to Slice a Dictionary in Python?

Conclusion

In this tutorial, we explored how to sort a Python dictionary by key alphabetically. I discussed two solutions such as using the sorted() function, using dict() and sorted() function. I also explained how to sort dictionaries with custom keys.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.