SoFunction
Updated on 2024-11-20

5 Ways to Sort Python Dictionary Keys and Values

Using the sorted() function

Use the sorted() function to sort a dictionary into a list of tuples, which are then sorted by the specified key or value.
Sample code for sorting by key

d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
sorted_d = dict(sorted((), key=lambda x: x[0]))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Sample code for sorting by value

d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
sorted_d = dict(sorted((), key=lambda x: x[1]))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Using the itemgetter() function

You can use the operator module's itemgetter() function to sort a dictionary, convert it to a list of tuples, and then sort it by the specified key or value.
Sample code for sorting by key

from operator import itemgetter
d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
sorted_d = dict(sorted((), key=itemgetter(0)))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Sample code for sorting by value

from operator import itemgetter
d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
sorted_d = dict(sorted((), key=itemgetter(1)))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Using the OrderedDict Class

Dictionaries can be sorted using the OrderedDict class of the collections module.OrderedDict is an ordered dictionary that preserves the order in which elements are inserted.
Sample code for sorting by key

from collections import OrderedDict
d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
sorted_d = OrderedDict(sorted((), key=lambda x: x[0]))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Sample code for sorting by value

from collections import OrderedDict
d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
sorted_d = OrderedDict(sorted((), key=lambda x: x[1]))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Using the zip() function

You can use the zip() function to convert the keys and values of a dictionary to a list, then sort the list by the specified key or value, and finally reassemble the dictionary from the sorted keys and values.

Sample code for sorting by key

d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
keys = list(())
values = list(())
sorted_keys = sorted(keys)
sorted_values = [d[k] for k in sorted_keys]
sorted_d = dict(zip(sorted_keys, sorted_values))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Sample code for sorting by value

d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
keys = list(())
values = list(())
sorted_values = sorted(values)
sorted_keys = [keys[(v)] for v in sorted_values]
sorted_d = dict(zip(sorted_keys, sorted_values))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Using the Pandas Library

You can use the Pandas library to convert a dictionary to a DataFrame, then sort it by a specified key or value, and finally convert the sorted DataFrame to a dictionary.
Sample code for sorting by key

import pandas as pd
d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
df = (list(()), columns=['fruit', 'count'])
sorted_df = df.sort_values('fruit')
sorted_d = dict(zip(sorted_df['fruit'], sorted_df['count']))
print(sorted_d)

The results are as follows:

在这里插入图片描述

Sample code for sorting by value

import pandas as pd
d = {'apple': 4, 'banana': 2, 'pear': 1, 'orange': 3}
df = (list(()), columns=['fruit', 'count'])
sorted_df = df.sort_values('count')
sorted_d = dict(zip(sorted_df['fruit'], sorted_df['count']))
print(sorted_d)

The results are as follows:

在这里插入图片描述

To this point this article on the 5 Python dictionary "key" and "value" of the sorting method of the article is introduced to this, more related Python dictionary key and value sorting content please search for my previous posts or continue to browse the following related articles I hope that everyone! I hope you will support me more in the future!