Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter a dict of dict

I new in Python and I am not sure it is a good idea to use dict of dict but here is my question. I have a dict of dict and I want to filter by the key of the inside dict:

a ={ 'key1' : {'id1' :[0,1,2] , 'id2' :[0,1,2], 'id3' :[4,5,6]}
     'key2' : {'id3' :[0,1,2] , 'id4' :[0,1,2]}
     'key3' : {'id3' :[0,1,2] , 'id1' :[4,5,6]}
   }

For exemple , I want to filter by 'id1' to have :

result = { 'key1' : {'id1' :[0,1,2] }
           'key3' : {'id1' :[4,5,6]}
         }

I have tried the filter method by I get all the value:

r = [('key1' ,{'id1' :[0,1,2] , 'id2' :[0,1,2], 'id3' :[4,5,6]})
     ('key3' , {'id3' :[0,1,2] , 'id1' :[4,5,6]})
   ]

Furthermore the filter method returns a list and I want to keep the format as a dict.

Thanks in advance

like image 683
Othman Avatar asked Jan 20 '23 18:01

Othman


2 Answers

Try this:

>>> { k: v['id1'] for k,v in a.items() if 'id1' in v }
{'key3': [4, 5, 6], 'key1': [0, 1, 2]}

For Python 2.x you might prefer to use iteritems() instead of items() and you'll still need a pretty recent python (2.7 I think) for a dictionary comprehension: for older pythons use:

dict((k, v['id1']) for k,v in a.iteritems() if 'id1' in v )

If you want to extract multiple values then I think you are best to just write the loops out in full:

def query(data, wanted):
    result = {}
    for k, v in data.items():
        v2 = { k2:v[k2] for k2 in wanted if k2 in v }
        if v2:
            result[k] = v2
    return result

giving:

>>> query(a, ('id1', 'id2'))
{'key3': {'id1': [4, 5, 6]}, 'key1': {'id2': [0, 1, 2], 'id1': [0, 1, 2]}}
like image 120
Duncan Avatar answered Jan 30 '23 22:01

Duncan


According to the precision you gave to Duncan, here is another filtering on a list using dictionary comprehension:

>>> my_list = ['id1', 'id2']
>>> {k1 : {k2: v2 for (k2, v2) in a[k1].iteritems() if k2 in my_list} for k1 in a}
{'key3': {'id1': [4, 5, 6]}, 'key2': {}, 'key1': {'id2': [0, 1, 2], 'id1': [0, 1, 2]}}

EDIT: you can also remove empty values with another dict compreehension, but that "begins" to be difficult to read... :-)

>>> {k3: v3 for k3, v3 in {k1 : {k2: v2 for (k2, v2) in a[k1].iteritems() if k2 in my_list} for k1 in a}.iteritems() if v3}
{'key3': {'id1': [4, 5, 6]}, 'key1': {'id2': [0, 1, 2], 'id1': [0, 1, 2]}}
like image 31
Emmanuel Avatar answered Jan 30 '23 23:01

Emmanuel