SoFunction
Updated on 2025-03-01

How to sort python dictionary

We know that Python's built-in dictionary data type is unordered, and the corresponding value is obtained through key. But sometimes we need to sort and output the items in the dictionary, which may be based on the key or the value. How many ways can we sort and output the content of dictionary? Some exciting solutions are collected below.

Python has two types of sorting data in containers, one is the container's own sort function, and the other is the built-in sorted function.

The only difference between the sort function and the sorted function is that sort is sorted in-place, and sorted generates a new sorted container.

1 Sort by Key value

#The easiest way, this is sorted by key value:def sortedDictValues1(adict): 
items = () 
() 
return [value for key, value in items] 
 
# Another one is sorted by key value, which seems to be faster than the previous onedef sortedDictValues2(adict): 
keys = () 
() 
return [dict[key] for key in keys] 
 
# Or sort by key value, it is said to be faster.  .  .  And it still applies when the key is tupledef sortedDictValues3(adict): 
keys = () 
() 
return map(, keys) 
 
#One line of statements:[(k,di[k]) for k in sorted(())] 
 
#Sort with key parameters (func) of sorted function:#Sort by keyprint sorted((), key=lambda d: d[0])

2 Sort by value

#Get a sorted by value, first put the key and value swap positions of the item into a list, and then according to the first value of each element of the list, that is, the original value value.Sort: 
def sort_by_value(d): 
items=() 
backitems=[[v[1],v[0]] for v in items] 
() 
return [ backitems[i][1] for i in range(0,len(backitems))] 
 
#It's still done with one line:[ v for v in sorted(())] 
 
#Sorting with lambda expressions is more flexible:sorted((), lambda x, y: cmp(x[1], y[1])), Or reverse order: 
sorted((), lambda x, y: cmp(x[1], y[1]), reverse=True) 
 
#Sort with the key parameters (func) of the sorted function: #Sort by valueprint sorted((), key=lambda d: d[1])

Knowledge point expansion:

Preparation knowledge:

In python, dictionary dictionary is a built-in data type, an unordered storage structure, and each element is a key-value pair:

For example: dict = {'username':'password', 'database':'master'}, where 'username' and 'database' are keys, and 'password' and 'master' are value. You can get the reference to the corresponding value value through d[key], but you cannot get the key through value.

For dictionnary, the following precautions are required:

a. The key of dictionary is case sensitive;

b. There cannot be duplicate keys in a dictionary;

c. dictionary is disordered and there is no concept of element order. They are just a simple arrangement of ordinals.

This is the end of this article about how to sort python dictionary. For more related python dictionary, please search for my previous article or continue browsing the related articles below. I hope you will support me in the future!