sorted() function

There are many ways to sort a python dictionary. Let’s look at 5 different ways to sort a python dictionary in this article.
- Using sorted() function
2. Sorting using reverse parameter
3. Sorting using the key parameter
4.Using sorted function in a dictionary comprehension
5. Using Counter
1. Using sorted() function
sorted() function is used to sort an iterable like a dictionary but it will return a sorted list.
Example 1: Sorting the dictionary
sorted(d1.items()) → d1.items() will sort the dictionary based on keys and will return list of tuples containing key-value pair
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.items())
#Output:[('apple', 2), ('banana', 3), ('cherry', 1)]
Example 2: Sorting the keys in the dictionary
sorted(d1.keys()) → It will sort the dictionary keys alone and return a list of sorted keys.
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.keys())
#Output:['apple', 'banana', 'cherry']
Example 3: Sorting the values in the dictionary
sorted(d1.values()) →It will sort the dictionary values alone and return a list of dictionary values.
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.values())
#Output:[1,2,3]
2. Sorting using reverse parameter
To sort the dictionary in reverse order (descending order), the reverse parameter is set to True.
Example 1: Sorting dictionary in reverse order
By default, if we set reverse=True, then it will sort based on dictionary keys in the reverse order(descending order)
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.items(),reverse=True)
#Output: [('cherry', 1), ('banana', 3), ('apple', 2)]
Example 2:Sorting dictionary keys in reverse order
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.keys(),reverse=True)
#Output:['cherry', 'banana', 'apple']
Example 3: Sorting dictionary values in reverse order.
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.values(),reverse=True)
#Output:[3,2,1]
3. Sorting using the key parameter
By default, the sorted() function will sort the dictionary based on keys. If we want to sort the dictionary based on values or any other parameter, then the “key” parameter is set.
Example 1: Sorting the dictionary based on values.
sorted(d1.items(),key=lambda x:x[1]) → Here key parameter is set as lambda function which will take the first index (values). The dictionary will be sorted based on values.
d1={'cherry':1,'apple':12,'banana':3}
sorted(d1.items(),key=lambda x:x[1])
#Output:[('cherry', 1), ('banana', 3), ('apple', 12)]
Example 2: Sorting dictionary based on length of keys.
sorted(d1.items(),key=lambda x: len(x[0])) → Here key parameter is set as a lambda function, which will take the length of keys and sort it.
d1={'cherry':1,'apple':2,'banana':3}
sorted(d1.items(),key=lambda x: len(x[0]))
#Output:[('apple', 2), ('cherry', 1), ('banana', 3)]
4. Using sorted function in a dictionary comprehension
Till now, the sorted function returns a list. If the sorted() function is used inside dictionary comprehension, it will return a sorted dictionary.
Example 1: Sorting dictionary based on keys and returns a sorted dictionary.
sort_keys={k:v for k,v in sorted(d1.items())} → Looping through key-value pairs (items) in the dictionary and sort it based on keys and return a sorted dictionary k:v
d1={'cherry':1,'apple':2,'banana':3}
#by default, sorted function, will sort based on keys.
sort_keys={k:v for k,v in sorted(d1.items())}
print(sort_keys)
#Output:{'apple': 2, 'banana': 3, 'cherry': 1}
Example 2: Sorting dictionary based on values and returns a sorted dictionary
d1={'cherry':1,'apple':12,'banana':3}
#key parameter is given as second element which is dict values
sort_values={k:v for k,v in sorted(d1.items(),key=lambda x:x[1])}
print (sort_values)
#Output:{'cherry': 1, 'banana': 3, 'apple': 12}
5. Using Counter
most_common() → sort the dictionary
If we create a Counter object, then the most_common() function will sort it based on values in descending order and will return a list of tuples.
from collections import Counter
d1={'cherry':1,'apple':12,'banana':3}
print(Counter(d1))
#Output:Counter({'apple': 12, 'banana': 3, 'cherry': 1})
c=Counter(d1)
print(c.most_common())
#Output: [('apple', 12), ('banana', 3), ('cherry', 1)]
Conclusion:
In this article, I have covered different ways to sort a python dictionary. sorted() function will return a list but if we want the return type as a dictionary, we can use the sorted () function inside dictionary comprehension.
Thanks for reading!
If you like to read more of my tutorials on Python and Data Science,
follow me on medium, Twitter
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee