#! /usr/bin/env python # -*- coding: utf-8 -*- import operator, itertools my_list = [ { "a": 1, "b": 1, "c": 3 }, { "a": 1, "b": 1, "c": 4 }, { "a": 1, "b": 11, "c": 3 }, { "a": 1, "b": 11, "c": 4 }, { "a": 2, "b": 1, "c": 3 }, { "a": 2, "b": 1, "c": 4 }, { "a": 2, "b": 11, "c": 3 }, { "a": 2, "b": 11, "c": 4 } ] # group my_list by "a" my_list.sort( key=operator.itemgetter('a') ) my_list_grouped = itertools.groupby( my_list, operator.itemgetter('a') ) my_list_grouped = list( my_list_grouped ) for k in [ 'first pass', 'second pass' ]: print k for my_item in my_list_grouped: print "\t" + str( my_item[0] ) # group by keyword "b"; need to get list first to be able to use 'sort' my_list2 = list( my_item[1] ) my_list2.sort( key=operator.itemgetter('b') ) my_list_grouped = itertools.groupby( my_list2, operator.itemgetter('b') ) for e in my_list_grouped: print "\t\t" + str( e[0] ) lll = list( e[1] ) for ee in lll: print "\t\t\t" + str( ee )