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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.