-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
  • 13,162
  • 23
  • 65
  • 103

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
  • 33,620
  • 7
  • 56
  • 73