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}