Skip to content
Python

値で辞書をソート

Python の辞書を値で降順にソート。

#dict#sorting#intermediate

Code

python
scores = {"Alice": 85, "Bob": 92, "Carol": 78}
sorted_scores = dict(
    sorted(scores.items(), key=lambda x: x[1], reverse=True)
)
print(sorted_scores)  # {'Bob': 92, 'Alice': 85, 'Carol': 78}