How To Sort A Dictionary In Python : Sort By Keys , Sort By Values

Last updated on Jul 15,2021 29.4K Views

How To Sort A Dictionary In Python : Sort By Keys , Sort By Values

edureka.co

Python programming language consists of primitive data types like list, dictionary, set, tuple etc. It also comes with a collections module which has specialized data structures like ChainMap, deque etc. It becomes easy to work with these data types as they consist of functions which makes the code efficient. Sort function is one such function for a dictionary in python. In this article we will discuss how we can sort a dictionary. Following are the concepts discussed in this blog:

What Is A Dictionary?

Dictionary is a collection data type which contains key value pairs just like a map in other programming languages.

mydictionary = { 'key1' : 'value 1' , 'key2' : 'value 2' , 'key3' : 'value 3'}
print(mydictionary)
Output : { 'key1' : 'value 1' , 'key2' : 'value 2' , 'key3' : 'value 3'}

Various Operations In A Dictionary

Following are the operations we can perform on a dictionary in python.

  1. clear
  2. copy
  3. fromkeys
  4. get
  5. items
  6. keys
  7. popitem
  8. pop
  9. setdefault
  10. update
  11. values

Need Of Sorting A Dictionary

How To Sort A Dictionary?

  1. Sorting by keys
  2. Sorting by values
  3. Custom sorting algorithms – string, number
  4. Reversing the sorted order

Sorting By Keys

We can use the in-built sorted function which will take any iterable and return a sorted list. We can use the keys in order to get a sorted dictionary in the ascending order.

a = {1:2 ,2:1 ,4:3 ,3:4 ,6:5 ,5:6 }
#this will print a sorted list of the keys
print(sorted(a.keys()))
#this will print the sorted list with items.
print(sorted(a.items()))
Output: [1,2,3,4,5,6]
[(1,2),(2,1),(3,4),(4,3),(5,6),(6,5)]

Sorting By Values

Just like keys, we can use the values as well.

a = {1:2 ,2:1 ,4:3 ,3:4 ,6:5 ,5:6 }
print(sorted.values()))
#this will print a sorted list of values.

Output: [ 1,2,3,4,5,6]

Custom Sorting Algorithm – String , Number

To perform more complex sorts, we can use other arguments in the sorted method.

day = { 'one' : 'Monday' , 'two' : 'Tuesday' , 'three' : 'Wednesday' , 'four' : 'Thursday' , 'five': 'Friday' , 'six' : 'Saturday' , 'seven': 'Sunday'}
print(day)
number = { 'one' : 1 , 'two' : 2 , 'three' : 3 , 'four' : 4 , 'five' : 5 , 'six' : 6 , 'seven' : 7}
print(sorted(day , key=number.__getitem__))
print([day[i] for i in sorted(day , key=number.__getitem__)])
Output: { 'one' : 'Monday' , 'two' : 'Tuesday' , 'three' : 'Wednesday' , 'four' : 'Thursday' , 'five': 'Friday' , 'six' : 'Saturday' , 'seven': 'Sunday'}
['one', 'two' , 'three' , 'four' , 'five' , 'six' , 'seven']
['Monday' , 'Tuesday', 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' , 'Sunday' ]

With the use of other arguments, we are able to sort the dictionary in a best way possible using both strings and numbers. We can reverse the order as well, below is an example to reverse the order of the sorted dictionary.

Reversing The Sorted Order

We can reverse the order of the sorted dictionary. Following is an example to reverse the order of the sorted dictionary.

a = {1:2 ,2:1 ,4:3 ,3:4 ,6:5 ,5:6 }
print(sorted(a.values() ,  reverse= True))
Output: [6,5,4,3,2,1]

In this blog, we have discussed how to sort a dictionary in python. Dictionary can be a optimized way of dealing with data which involves key value pairs. It becomes easier to work on dictionaries since they are mutable in nature and have a search time complexity less than that of a list.

Data types in python is an important fundamental concept which stands python apart. With ease of access and improved readability it helps in other python applications such as analytics and data science. To master python enroll to Edureka’s Python certification program and kick-start your learning.

Have any questions? mention them in the comments, we will get back to you as soon as possible.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Prompt Engineering Explained