What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

Python: Sort Dictionary by Value & Key (with Code)

  • Feb 15, 2021
  • 3 Minute Read
Python: Sort Dictionary by Value & Key (with Code)

Dictionary is one of the data structures in the python language. It helps to store the data in the value key pair. In this article, we will learn in python to sort a dictionary by value or by key. So, let’s started!

Need for Sorting in Dictionary

Dictionaries are python’s data structure that is more generally known as an associate array. A dictionary consists of a collection of key-value pairs. Each key-value pair maps the key to its respective value. Python dictionary makes it easier to read and change data. You can define a dictionary by enclosing a comma-separating list of key-value pair in curly braces({}). A colon(:) is used to separate the key from its value pair. While creating a dictionary in python, we always find that it is not sorted. But if we sort the dictionary, then it is effortless to access the data.

Sort dictionary by value python

Python sorted() method can be along with lambda function to sort a dictionary by value in python in a predefined order.

Here, the python lambda function creates an anonymous function that helps to optimize the codes.

The syntax of the lambda function will be as given below:

lambda arguments: expression

Example:

dictionary = { 'A':1,'ABC':3,'ABCD':4,'AB':2 }
print("Dictionary: ", dictionary)
sort_dictionary= dict(sorted(dictionary.items(), key=lambda item: item[1])) 
print("Sorted Dictionary by value: ", sort_dictionary)

Now here we use the items() function which allows you to retrieve the items in the dictionary. We use this function in combination with the sorted() function and a custom key parameter to sort a dictionary by value.  

The output of the above code is as given below:

 Dictionary: {'A': 1, 'ABC': 3, 'ABCD': 4, 'AB': 2}

 Sorted Dictionary by value: {'A': 1, 'AB': 2, 'ABC': 3, 'ABCD': 4}

Sort dictionary by key python

We can also sort the dictionary by keys in python too. Here, too we will use the sorted() method along with the lambda function to sort a dictionary by key.

Example:

dictionary = {'john':'1','alice':'90','rook':'45','emma':'22','roz':'11'} 
dictionary1 = sorted(dictionary.items())
print(dictionary1) 

Here, we create the dictionary and then use the sorted() function and combine it with items() to get each item from the dictionary and then sort it accordingly by key.

The output of the above code is as given below:

 [('alice', '90'), ('emma', '22'), ('john', '1'), ('rook', '45'), ('roz', '11')

So, this is how to in python sort dictionary using two different methods.

Conclusion

Here, in this article, we learned sorting dictionary in python and its need to sort the data. We learned about two different methods, by key, and by value.

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Shivali Bhadaniya
I'm Shivali Bhadaniya, a computer engineer student and technical content writer, very enthusiastic to learn and explore new technologies and looking towards great opportunities. It is amazing for me to share my knowledge through my content to help curious minds.