Created
December 7, 2022 15:55
-
-
Save Shalabyelectronics/f15c8267d9d4a35f6656428731f1866f 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
glossary = { | |
"fruits": ["Apple", "Orange", "Banana"], | |
"vegetables": ["Cucumber", "Lemon", "Tomato"], | |
"sweets": ["Mars", "Kitkat", "Galaxy"] | |
} | |
# This about invers dictionary values from list to become a keys and their keys became a values that refer to them | |
def invert_dict(d): | |
inverse = dict() | |
for key in d: | |
val_list = d[key] | |
for val in val_list: | |
if val not in inverse: | |
inverse[val] = [key] | |
else: | |
inverse[val].append(key) | |
print(inverse) | |
invert_dict(glossary) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment