Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

Python invert dictionary

# Invert a dictionary that can have several keys mapping to the same value
# In the inverse dictionary every value is mapped to the list of its keys

D={'a':1, 'b':1, 'c':2, 'd':2, 'e':3} 
D_inv={} 
for k,v in D.items(): D_inv[v]=D_inv.get(v,[])+[k]
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Python #invert #dictionary
ADD COMMENT
Topic
Name
8+1 =