>>> from operator import itemgetter
>>> lis = [{'name':'Accra','url':''},{'name':'Dubai','url':''},{'name':'Birmingham','url':''},{'name':'Dhaka','url':''},{'name':'Abu Dhabi','url':''},{'name':'Lahore','url':''},{'name':'Karachi','url':''},{'name':'Kathmandu','url':''},{'name':'SEA','url':''}]
>>> sorted(lis, key=itemgetter('name'))
[{'url': '', 'name': 'Abu Dhabi'}, {'url': '', 'name': 'Accra'}, {'url': '', 'name': 'Birmingham'}, {'url': '', 'name': 'Dhaka'}, {'url': '', 'name': 'Dubai'}, {'url': '', 'name': 'Karachi'}, {'url': '', 'name': 'Kathmandu'}, {'url': '', 'name': 'Lahore'}, {'url': '', 'name': 'SEA'}]
要对列表进行就地排序,请使用:
lis.sort(key=itemgetter('name'))