Python Programming (134 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (32 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (70 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)

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

Last updated on Jul 15,2021 27.3K Views

32 / 62 Blog from Python Fundamentals

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.

  • Dictionary is mutable in nature, which means changes can be made even after declaring a dictionary in python.
  • It is unordered and allows duplicate entries, only in the values since the keys has to be distinct.
  • The values are accessed using the keys as indexes in a dictionary.
  • A dictionary is declared in curly brackets.
mydictionary = { 'key1' : 'value 1' , 'key2' : 'value 2' , 'key3' : 'value 3'}
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

  • A dictionary has a O(1) search time complexity whereas a list has a O(n) search time complexity, which makes dictionary is a viable option wherever necessary.
  • A sorted dictionary yields better understanding and clarity in handling the operations.
  • Sorting helps in making efficient analysis while working with any data structure.

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
#this will print the sorted list with items.
Output: [1,2,3,4,5,6]

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 }
#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'}
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 Certification Training Course
Course NameDate
Python Certification Training Course

Class Starts on 15th October,2022

15th October

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

Class Starts on 12th November,2022

12th November

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories

Send OTP
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

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