Find all Key-Elements by the same Value in Dicts

Here's a naive implementation. Someone with better Python skills can probably make it more concise and awesome.

dict = { 'abc':'a', 'cdf':'b', 'gh':'a', 'fh':'g', 'hfz':'g' }

new_dict = {}
for pair in dict.items():
    if pair[1] not in new_dict.keys():
        new_dict[pair[1]] = []

    new_dict[pair[1]].append(pair[0])

print new_dict

This produces

{'a': ['abc', 'gh'], 'b': ['cdf'], 'g': ['fh', 'hfz']}

If you are fine with lists instead of tuples in the new dictionary, you can use

from collections import defaultdict
some_dict = { 'abc':'a', 'cdf':'b', 'gh':'a', 'fh':'g', 'hfz':'g' }
new_dict = defaultdict(list)
for k, v in some_dict.iteritems():
    new_dict[v].append(k)

If you want to avoid the use of defaultdict, you could also do

new_dict = {}
for k, v in some_dict.iteritems():
    new_dict.setdefault(v, []).append(k)