Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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'}
Following are the operations we can perform on a dictionary in python.
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)]
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]
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.
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.
Course Name | Date | Details |
---|---|---|
Python Programming Certification Course | Class Starts on 30th November,2024 30th November SAT&SUN (Weekend Batch) | View Details |
Python Programming Certification Course | Class Starts on 28th December,2024 28th December SAT&SUN (Weekend Batch) | View Details |
edureka.co