Sorting and Ranking
Order by the value you mean, and remember that sorted returns a new list.
Sorting by a field
key says which part of each row to order by. reverse turns it around. sorted gives you a new list; .sort() changes the one you have and returns None.
rows = [{"name": "Ada", "score": 92}, {"name": "Alan", "score": 79}]
best = sorted(rows, key=lambda r: r["score"], reverse=True)
print([r["name"] for r in best])
The None that catches everyone
names = names.sort() throws your list away and keeps None. It is the single most common list mistake, and nothing raises until you use the result.
names = ["Grace", "Ada"]
print(names.sort())
print(names)
print(sorted(["Grace", "Ada"]))
Exercise
Try It YourselfWrite top_names(rows, n) that returns the names of the n highest scorers, highest first.
Press Run to see output