I have a nested dictionary which looks like:

{ 

    A:{I:{key1:value1,key2:value2},
       II:{key1:value2, key2:value5, key3:value6}},


    B:{I:{key1:value1,key2:value2},
       III:{key1:value2, key2:value5, key3:value6}
       IV:{key1:value9, key2:value7, key3:value8}},
    C:{V:{key1:value11,key2:value22},
       VI:{key1:value12, key2:value15, key3:value16}
       VII:{key1:value19, key2:value17, key3:value18}}
}

My objective is to retrieve the keys ( A,B,C and I,II,III,IV,V,VI,VII) for the occurences of a value. So for example, if I am querying the dictionary for "value2", I should get the result as

{A:[I,II],B:[I,III]}

What is the most efficient way to do this query?

Edited 4 Years Ago by Sunciti: correction

Can not test with your code as we have not definition of the myriad variables in your dictionary as values or keys, and there is some comma missing but it should be something like:

print({key:d[key].keys() for key in d})

If I put strings instead of your variables, I get:

 d = {'A': {'I': {'key2': 'value2', 'key1': 'value1'}, 'II': {'key3': 'value6', 'key2': 'value5', 'key1': 'value2'}}, 'C': {'VI': {'key3': 'value16', 'key2': 'value15', 'key1': 'value12'}, 'VII': {'key3': 'value18', 'key2': 'value17', 'key1': 'value19'}, 'V': {'key2': 'value22', 'key1': 'value11'}}, 'B': {'I': {'key2': 'value2', 'key1': 'value1'}, 'III': {'key3': 'value6', 'key2': 'value5', 'key1': 'value2'}, 'IV': {'key3': 'value8', 'key2': 'value7', 'key1': 'value9'}}}
print({key:d[key].keys() for key in d})
'''Output:
{'A': ['I', 'II'], 'C': ['VI', 'VII', 'V'], 'B': ['I', 'III', 'IV']}
''' 

Are you trying to do something with roman numbers?

Edited 4 Years Ago by pyTony: code

Hi pyTony- Thanks for your prompt response. Apologies for the confusion regarding commas and variables. Actually I meant all those to be strings.

I have posted the new code below:

  dict=

      {
        'A':{'I':{'key1':'value1','key2':'value2'},
            'II':{'key1':'value2', 'key2':'value5', 'key3':'value6'}},
        'B':{'I':{'key1':'value1','key2':'value2'},
            'III':{'key1':'value2', 'key2':'value5', 'key3':'value6'},
             'IV':{'key1':'value9', 'key2':'value7', 'key3':'value8'}},
        'C':{'V':{'key1':'value11','key2':'value22'},
             'VI':{'key1':'value12', 'key2':'value15', 'key3':'value16'},
            'VII':{'key1':'value19', 'key2':'value17', 'key3':'value18'},}
      }

I defined the following procedure called search to find out a value from the dictionary and return the ALPHABET and the ROMAN corresponding to the VALUE.

def search(dict, value):
    a = {}
    for k1,ALPHABET in dict.items():
        for k2,ROMAN in ALPHABET.items():
            for k3,attr in ROMAN.items():
                if value == attr:
                    if a =={}:
                        a = {k1:[k2]}
                    elif k1 in a:
                        pass
                    elif k1 not in a:
                        a1={k1:[k2]}
                        a.update(a1)
    return a

print search(dict,'value2')

I am getting the result:

    {'A': ['I'], 'B': ['I']}

when I am expecting:

    {'A': ['I','II'], 'B': ['I','III']}

That is because I don't know how to update the dictionary for the first "elif" condition- where I had inserted a "pass".

So finally my questions:

  1. What should be the code in "pass" to update the dictionary?
  2. Is there a better/efficient code to achieve the same result?

    BTW, I get an invalid syntax error for

    " print({key:d[key].keys() for key in d})"

    when I ran your code.

Edited 4 Years Ago by Sunciti: correction

BTW, I get an invalid syntax error for

You are not running then Python 2.72 or Python 3.2.2, use dict() (do not use dict as variable name or you are overriding the builtin typy with the variable) with generator instead:

print(dict((key, d[key].keys()) for key in d))

Edited 4 Years Ago by pyTony: dict bad variable name

Thanks again pyTony. But the print statement returns the ALPHABET-ROMAN heirarchy for all keys in the dictionary. I was searching for the ALPHABET-ROMAN hierarchy for the keys which had value = 'value2'. Thanks again in advance :-)

Something like this then (untested):

    print(dict((key, [k for k, value in d[key].items() if value == 'value2']) for key in d))
This article has been dead for over six months. Start a new discussion instead.