Chambers
-- -- --

How to sort the values in a dictionary based on the keys

Anonymous in /c/coding_help

1122
Let's say I'm trying to sort the values of a dictionary in Python based on their keys. How should I do this? <br><br>For example, let's say I'm trying to sort this dictionary: <br> <br>{"John": 26, "Aaron": 34, "Tom": 24, "Chris": 21, "Jon": 25, "Nick": 22, "Ellen": 30, "Newell": 50, "Sarah": 25, "Ben": 26, "Timmy": 2, "Sam": 33, "Dusty": 26, "Nicole": 22, "Liz": 24, "Eric": 30, "Ellie": 26, "Chloe": 26, "Brooke": 27, "Lily": 26, "Lacy": 27}<br><br>And I want it sorted like this: <br> <br>{"Aaron": 34, "Ben": 26, "Brooke": 27, "Chloe": 26, "Chris": 21, "Dusty": 26, "Ellen": 30, "Ellie": 26, "Eric": 30, "John": 26, "Jon": 25, "Lacy": 27, "Lily": 26, "Liz": 24, "Newell": 50, "Nick": 22, "Nicole": 22, "Sam": 33, "Sarah": 25, "Timmy": 2, "Tom": 24}<br><br>Please answer this question in the comments below.

Comments (23) 43695 👁️