Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python3 

2"""Solution to chapter 3, exercise 11: alphabetize_names""" 

3 

4import operator 

5 

6 

7PEOPLE = [{'first': 'Reuven', 'last': 'Lerner', 

8 'email': 'reuven@lerner.co.il'}, 

9 {'first': 'Donald', 'last': 'Trump', 

10 'email': 'president@whitehouse.gov'}, 

11 {'first': 'Vladimir', 'last': 'Putin', 

12 'email': 'president@kremvax.ru'} 

13 ] 

14 

15 

16def alphabetize_names(list_of_dicts): 

17 """Take a list of dicts describing people, 

18each with first/last/email as keys. 

19 

20Return a new list of dicts, 

21sorted first by last name and then by first name. 

22 

23If passed an empty list, then return an empty list. 

24""" 

25 return sorted(list_of_dicts, key=operator.itemgetter('last', 'first'))