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?
- Various Operations In A Dictionary
- Need Of Sorting A Dictionary
- How To Sort A Dictionary?
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'} 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.
- clear
- copy
- fromkeys
- get
- items
- keys
- popitem
- pop
- setdefault
- update
- 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?
- Sorting by keys
- Sorting by values
- Custom sorting algorithms – string, number
- 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.