Nov 17 sec 01 lst = [("a", 6, "mo"), ("z", 2, "flo"), ("v", 9, "bo"), ("g", 7, "joe")] import operator y = sorted(lst) print y [('a', 6, 'mo'), ('g', 7, 'joe'), ('v', 9, 'bo'), ('z', 2, 'flo')] y = sorted(lst, key=operator.itemgetter(2)) print y [('v', 9, 'bo'), ('z', 2, 'flo'), ('g', 7, 'joe'), ('a', 6, 'mo')] lst.append(('a', 7, "bo")) print lst [('a', 6, 'mo'), ('z', 2, 'flo'), ('v', 9, 'bo'), ('g', 7, 'joe'), ('a', 7, 'bo')] z = sorted(lst, key=operator.itemgetter(1),reverse=True) print z [('v', 9, 'bo'), ('g', 7, 'joe'), ('a', 7, 'bo'), ('a', 6, 'mo'), ('z', 2, 'flo')] # This next line sorts list z that was sorted above on the second items # (those in slot 1) in the tuple. This time it sorts it on the first item # in the tuples. Note that for 'a' which has two tuples, the items 7 and 6 # are still in the order that resulted from the first sort. z = sorted(z, key=operator.itemgetter(0)) print z [('a', 7, 'bo'), ('a', 6, 'mo'), ('g', 7, 'joe'), ('v', 9, 'bo'), ('z', 2, 'flo')] # Here we sort the original list lst on the first item in the tuples. With the # two a's, note that the second item in each tuple, the 6 and 7 are in the same # order they were in the original list. z = sorted(lst, key=operator.itemgetter(0)) print z [('a', 6, 'mo'), ('a', 7, 'bo'), ('g', 7, 'joe'), ('v', 9, 'bo'), ('z', 2, 'flo')]