I often find myself with a list of dictionaries and I will need to sort them by a specific key contained in all of the dictionaries. The purpose of this post is to show a quick and easy way to sort a list of dictionaries. Below is the code to accomplish this.

from operator import itemgetter

my_list = [{'first': 'tony', 'last': 'danza'},
           {'first': 'david', 'last': 'parker'},
           {'first': 'jordan', 'last': 'davis'}]

list_sorted_by_last = sorted(my_list, key=itemgetter('last'))
print(list_sorted_by_last)

And if we run this code we get the output below.

[{'last': 'parker', 'first': 'david'}, {'last': 'davis', 'first': 'jordan'}, {'last': 'danza', 'first': 'tony'}]