I have two dictionaries:
first_dict = {'a': ['1', '2', '3'],
'b': ['4', '5'],
'c': ['6'],
}
second_dict = {'1': 'wqeewe',
'2': 'efsafa',
'4': 'fsasaf',
'6': 'kgoeew',
'7': 'fkowew'
}
I want to have a third dict that will contain the key of second_dict and its corresponding value from first_dict's key. This way, I will have :
third_dict = {'1' : 'a',
'2' : 'a',
'4' : 'b',
'6' : 'c',
'7' : None,
}
here is my way:
def key_return(name):
for key, value in first_dict.items():
if name == value:
return key
if isinstance(value, list) and name in value:
return key
return None
reference: Python return key from value, but its a list in the dictionary
However, I wondering that the another way using dict.get() or something else. Any help would be appreciated. Thanks.