In [1]:
import pprint

In [2]:
l = [{'Name': 'Alice', 'Age': 40, 'Point': 80}, 
     {'Name': 'Bob', 'Age': 20},
     {'Name': 'Charlie', 'Age': 30, 'Point': 70}]

pprint.pprint(l)


[{'Age': 40, 'Name': 'Alice', 'Point': 80},
 {'Age': 20, 'Name': 'Bob'},
 {'Age': 30, 'Name': 'Charlie', 'Point': 70}]

In [3]:
# l.sort()
# TypeError: '<' not supported between instances of 'dict' and 'dict'

In [4]:
l.sort(key=lambda x: x['Age'])

pprint.pprint(l)


[{'Age': 20, 'Name': 'Bob'},
 {'Age': 30, 'Name': 'Charlie', 'Point': 70},
 {'Age': 40, 'Name': 'Alice', 'Point': 80}]

In [5]:
l.sort(key=lambda x: x['Name'])

pprint.pprint(l)


[{'Age': 40, 'Name': 'Alice', 'Point': 80},
 {'Age': 20, 'Name': 'Bob'},
 {'Age': 30, 'Name': 'Charlie', 'Point': 70}]

In [6]:
# l.sort(key=lambda x: x['Point'])
# KeyError: 'Point'

In [7]:
# l.sort(key=lambda x: x.get('Point'))
# TypeError: '<' not supported between instances of 'int' and 'NoneType'

In [8]:
l.sort(key=lambda x: x.get('Point', 0))

pprint.pprint(l)


[{'Age': 20, 'Name': 'Bob'},
 {'Age': 30, 'Name': 'Charlie', 'Point': 70},
 {'Age': 40, 'Name': 'Alice', 'Point': 80}]

In [9]:
l.sort(key=lambda x: x.get('Point', 100))

pprint.pprint(l)


[{'Age': 30, 'Name': 'Charlie', 'Point': 70},
 {'Age': 40, 'Name': 'Alice', 'Point': 80},
 {'Age': 20, 'Name': 'Bob'}]

In [10]:
l.sort(key=lambda x: x['Name'], reverse=True)

pprint.pprint(l)


[{'Age': 30, 'Name': 'Charlie', 'Point': 70},
 {'Age': 20, 'Name': 'Bob'},
 {'Age': 40, 'Name': 'Alice', 'Point': 80}]

In [11]:
l_sorted = sorted(l, key=lambda x: x['Age'], reverse=True)

pprint.pprint(l_sorted)


[{'Age': 40, 'Name': 'Alice', 'Point': 80},
 {'Age': 30, 'Name': 'Charlie', 'Point': 70},
 {'Age': 20, 'Name': 'Bob'}]