Created
August 23, 2021 19:25
-
-
Save rgiaviti/c9c66337384938c8335730a886d367b7 to your computer and use it in GitHub Desktop.
Ordering a list of dicts with Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
players = [ | |
{"name": "Pelé","age": 80}, | |
{"name": "Beckenbauer", "age": 75}, | |
{"name": "Baresi", "age": 61}, | |
{"name": "Zamorano", "age": 54}, | |
{"name": "Mauro Shampoo", "age": 64} | |
] | |
sorted_players_by_name = sorted(players, key=lambda k: k["name"]) | |
sorted_players_by_age = sorted(players, key=lambda k: k["age"]) | |
print(sorted_players_by_name) | |
print(sorted_players_by_age) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment