#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Demonstrate how insertion order affects dictonaries and sets. """ #end_pymotw_header def group_by_length(words): """Returns a dictionary grouping words into sets by length. >>> grouped = group_by_length([ 'python', 'module', 'of', 'the', 'week' ]) >>> grouped == { 2:set(['of']), ... 3:set(['the']), ... 4:set(['week']), ... 6:set(['python', 'module']), ... } True """ d = {} for word in words: s = d.setdefault(len(word), set()) s.add(word) return d