- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to sort a dictionary in Python?
A dictionary is a data structure that consists of key and value pairs. We can sort a dictionary using two criteria −
Sort by key − The dictionary is sorted in ascending order of its keys. The values are not taken care of.
Sort by value − The dictionary is sorted in ascending order of the values.
Method 1 − Sort the dictionary by key
In this approach, the dictionary is sorted in ascending order of its keys.
Input:
{2:90, 1: 100, 8: 3, 5: 67, 3: 5}
Output:
{1:100, 2:90, 3:5, 5:67, 8:3}
As shown above, we can see the dictionary is sorted according to its keys.
Example
dic={2:90, 1: 100, 8: 3, 5: 67, 3: 5} dic2={} for i in sorted(dic): dic2[i]=dic[i] print(dic2)
Output
{1: 100, 2: 90, 3: 5, 5: 67, 8: 3}
Explanation of code line wise
Declare a dictionary which is to be sorted
Declare an empty dictionary where sorted key value pairs are to be added
sorted(dic) has all the keys of dic in sorted order.It only has the keys and not keyvalue pairs. sorted(dic) will have [1,2,3,5,8]
For each key in sorted order, add the key and the corresponding value into dic2.
dic2 has all the key-value pairs in sorted order of keys
Method 2 − Sort dictionary by values
In this approach, the dictionary is sorted in ascending order of values.
Input:
{2:90, 1: 100, 8: 3, 5: 67, 3: 5}
Output:
{8:3, 3:5 ,5:67 , 2:90, 1:100}
As shown above, we can see the dictionary is sorted according to its values.
We use the sorted() and items() methods together to sort the dictionary by value.
The items() is used to retrieve the items or values of the dictionary.
The key=lambda x: x[1] is a sorting mechanism that uses a lambda function.
This gives us key value pairs ehich are then converted into a dictionary using dict().
Example
dic={2:90, 1: 100, 8: 3, 5: 67, 3: 5} dic2=dict(sorted(dic.items(),key= lambda x:x[1])) print(dic2)
Output
{8: 3, 3: 5, 5: 67, 2: 90, 1: 100}
- Related Articles
- How to sort a nested Python dictionary?
- How to sort a dictionary in Python by keys?
- How to sort a dictionary in Python by values?
- How to sort a Python dictionary by datatype?
- How to sort a Python dictionary by value?
- Sort Dictionary key and values List in Python
- How to define a Python dictionary within dictionary?
- How to create a dictionary in Python?
- How to convert a String representation of a Dictionary to a dictionary in Python?
- How do I sort a list of dictionaries by values of the dictionary in Python?
- C++ Program to Sort a Dictionary By Values
- How to convert the string representation of a dictionary to a dictionary in python?
- How to convert Javascript dictionary to Python dictionary?
- How to Sort Elements in Lexicographical Order (Dictionary Order) in Golang?
- Swift Program to sort the Dictionary
