>>> def invert(table): ...index = { } #empty dictionary ...for key in table.keys(): ......value = table[key] ......if not index.has_key(value): .........index[value] = [] # empty list ......index[value].append(key) ...return index
>>> phonebook() = {'guido': 4127, 'sjoerd': 4127, 'jack' : 4098} >>> phonebook['dcab'] = 4147 >>> inverted_phonebook = invert(phonebook) >>> print inverted_phonebook { 4098: ['jack'], 4127: ['guido', 'sjoerd'], 4147: ['dcab'] }