Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

invert dictionary python

inv_map = {v: k for k, v in my_map.items()}
Comment

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]
Comment

invert dictionary python

orig = { 1:'A',  2:'B',  3:'C' }
new = dict(zip(orig.values(), orig.keys()))
new == {'A': 1, 'B': 2, 'C': 3} #True
Comment

invert a dictionary python

d = {'a':1, 'b':2, 'c':3}
inverted_d = {}

for k, v in d.items():
  inverted_d[v] = k
Comment

# invert a dictionary

# invert a dictionary
def inv_dict(obj):
  return { value: key for key, value in obj.items() }

d = {'apple':1, 'oranges':2, 'bananas':3}
print(d)
print(inv_dict(d))


# Output:
# {1: 'apple', 2: 'oranges', 3: 'bananas'}
Comment

Inverting a Dictionary

my_dict = {
  'Izuku Midoriya': 'One for All', 
  'Katsuki Bakugo': 'Explosion', 
  'All Might': 'One for All', 
  'Ochaco Uraraka': 'Zero Gravity'
}

# Use to invert dictionaries that have unique values
my_inverted_dict = dict(map(reversed, my_dict.items()))

# Use to invert dictionaries that have unique values
my_inverted_dict = {value: key for key, value in my_dict.items()}

# Use to invert dictionaries that have non-unique values
from collections import defaultdict
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}

# Use to invert dictionaries that have non-unique values
my_inverted_dict = dict()
for key, value in my_dict.items(): 
  my_inverted_dict.setdefault(value, list()).append(key)

# Use to invert dictionaries that have lists of values
my_dict = {value: key for key in my_inverted_dict for value in my_inverted_dict[key]}
Comment

PREVIOUS NEXT
Code Example
Python :: pyqt latex 
Python :: pyqt5 pylatex 
Python :: convert hex to decimal python 
Python :: create directory in python 
Python :: append to csv python 
Python :: degrees to radians python 
Python :: drop multiple columns in python 
Python :: You did not provide the "FLASK_APP" environment variable 
Python :: download image python 
Python :: how to sort list in descending order in python 
Python :: python hello wrold 
Python :: python change format of datetime 
Python :: ModuleNotFoundError: No module named ‘click’ 
Python :: dask show progress bar 
Python :: discord python wait for user input 
Python :: python deepcopy 
Python :: get local python api image url 
Python :: python read mp3 livestream 
Python :: how to execute a cmd command in python 
Python :: ImportError: No module named pip --Windows 
Python :: openpyxl get last non empty row 
Python :: q django 
Python :: pyspark dataframe to single csv 
Python :: telethon invite to group 
Python :: openpyxl delete column by name 
Python :: jupyter notebook set default directory 
Python :: torchviz 
Python :: how to find the multiples of a number in python 
Python :: delete the duplicates in python 
Python :: python fizzbuzz 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =