Skip to content

Instantly share code, notes, and snippets.

@stober
Created May 10, 2011 18:13
Show Gist options
  • Save stober/965024 to your computer and use it in GitHub Desktop.
Save stober/965024 to your computer and use it in GitHub Desktop.
Reversing a Dictionary (Alternate)
def create_rdict(d):
"""
Return a dictionary with each value mapping to a set of keys.
"""
rd = {}
for (key,value) in d.items():
if rd.has_key(value):
rd[value].add(key)
else:
rd[value] = set([key])
return rd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment