sort python dictionary keys

Sort Python Dictionary Keys and Values

In this tutorial, we will look at how to sort the keys and values of a Python dictionary with the help of some examples.

Are dictionaries in Python ordered?

Normal Python dictionaries are unordered. That is, there’s no inherent order to the keys or values present in a dictionary. If you want an inherent order to a dictionary’s items, use OrderedDict instead. OrderedDict can be imported from the collections module in Python.

Note that from Python 3.7 and above, dictionaries are ordered by their keys by default.

In this tutorial, we will look at how to sort a dictionary’s keys and values separately and not inside the dictionary itself.

Sort Dictionary Keys

You can use Python built-in sorted() function to sort the keys of a dictionary. Use the dictionary keys() function to get the dictionary keys and then apply the sorted() function to sort the returned keys.

Let’s look at an example.

# create a dictionary
salary_info = {
    "Brandon": 70000,
    "Adam": 80000,
    "Charles": 75000
}
# sort the dictionary keys
print(sorted(salary_info.keys()))

Output:

['Adam', 'Brandon', 'Charles']

You can see that the keys are present in sorted order.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

You can also sort the keys in descending order. Pass reverse=True to the sorted() function.

# create a dictionary
salary_info = {
    "Brandon": 70000,
    "Adam": 80000,
    "Charles": 75000
}
# sort the dictionary keys in descending order
print(sorted(salary_info.keys(), reverse=True))

Output:

['Charles', 'Brandon', 'Adam']

The keys in the output are sorted in descending order.

Sort Dictionary Values

You can similarly use the sorted() function to sort a dictionary’s values. Use the dictionary values() function to get the dictionary values and then apply the sorted() function to sort the returned values.

Let’s look at an example.

# create a dictionary
salary_info = {
    "Brandon": 70000,
    "Adam": 80000,
    "Charles": 75000
}
# sort the dictionary values
print(sorted(salary_info.values()))

Output:

[70000, 75000, 80000]

The values are sorted in ascending order.

You can also sort the values in descending order. Pass reverse=True to the sorted() function.

# create a dictionary
salary_info = {
    "Brandon": 70000,
    "Adam": 80000,
    "Charles": 75000
}
# sort the dictionary values in descending order
print(sorted(salary_info.values(), reverse=True))

Output:

[80000, 75000, 70000]

The values in the output are sorted in descending order.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top