#!/usr/bin/python # Ivan Novick # Feb-2, 2008 # dictionary_sort.py # show how to loop through the items in a dictionary # map of people and their ages people = {} people["igor"] = 32 people["sarah"] = 62 people["fred"] = 87 people["samantha"] = 29 # iterate through the keys in the people dictionary for p in people.keys(): print "%s is %d years old" % (p, people[p]) print "\nnow print the list in sorted order\n" # use the "sorted" builtin function for p in sorted(people.keys()): print "%s is %d years old" % (p, people[p])