Skip to content

Instantly share code, notes, and snippets.

@SaraM92
Created September 24, 2021 06:26
Show Gist options
  • Save SaraM92/35dbae10a0ae61ffa00feb5663fe4081 to your computer and use it in GitHub Desktop.
Save SaraM92/35dbae10a0ae61ffa00feb5663fe4081 to your computer and use it in GitHub Desktop.
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