-3

I have list:

[
    {'name': 'peter', 'age': 41, 'value': 1},
    {'name': 'jon', 'age': 31, 'value': 5},
    {'name': 'alan', 'age': 23, 'value': 3}
]

How to sort this list by 'age'?

Nips
  • 12,112
  • 22
  • 60
  • 99

1 Answers1

1

You can use a lambda function and one of the following functions to sort:

If you want to sort in-place (modify the list):

L.sort(key = lambda d:d['age'])

If you want to create a new sorted list:

print sorted(L, key = lambda d:d['age'])
Christian Tapia
  • 32,670
  • 6
  • 50
  • 72