Skip to content

Instantly share code, notes, and snippets.

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