Created
September 24, 2021 06:26
-
-
Save SaraM92/35dbae10a0ae61ffa00feb5663fe4081 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_dict = { | |
"brand": "Ford", | |
"model": "Mustang", | |
"year": 1964 | |
} | |
#Invert the dictionary based on its content | |
#1- If we know all values are unique. | |
my_inverted_dict = dict(map(reversed, my_dict.items())) | |
#2- If non-unique values exist | |
from collections import defaultdict | |
my_inverted_dict = defaultdict(list) | |
{my_inverted_dict[v].append(k) for k, v in my_dict.items()} | |
#3- If any of the values are not hashable | |
my_dict = {value: key for key in my_inverted_dict for value in my_inverted_dict[key]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment