How to determine key name of outer dictionary knowing values in nested dictionary

30 views Asked by At

So I have a dictionary like below:

{
  'outerkey1': 
    {'innerkey1': 'value1'},
  'outerkey2': 
    {'innerkey2': 'value2'}
}

I know the value of 'value2', which I can determine the key name is 'innerkey2', but how can I know it's from 'outerkey2'?

1

There are 1 answers

0
BruceWayne On

You could just loop through the dictionary:

my_dict = {'outerkey1':{'innerkey1':'value1'}, 'outerkey2':{'innerkey2':'value2'}}

lookup = "value2"

for key, val in my_dict.items():
    for x, y in val.items():
        if y == lookup:
            print(key)