Reputation: 13
While searching for an answer to this question I found that many posts were either concerned more with ordering by keys of each dictionary in a list or the dictionaries within the lists had descriptions for each value like:
[{'name': 'john'}, {'name': 'sam'}]
possibly making it easier to order each dictionary.
I have a list of dictionaries in the following format:
[{'Emma': 20}, {'Jake': 15}, {'John': 22}]
How can I order this list by each users age using sorted and lambda only (if possible)?
Any help would be much appreciated.
Upvotes: 1
Views: 85
Reputation: 7812
You can use next()
to get first of dict values:
source = [{'Emma': 20}, {'Jake': 15}, {'John': 22}]
sorted_source = sorted(source, key=lambda x: next(iter(x.values())))
Upvotes: 3