Home > Back-end >  Python Search for a specific value in a nested dictionary and get one dictionary including the value
Python Search for a specific value in a nested dictionary and get one dictionary including the value

Time:02-04

I am trying to get a dictionary that includes a specific value from nested dictionaries.

My multiple dictionaries look like this:

MyDto = [{'app' : 'chrome',
'id': 'adetwt',
'name': 'John',
'selection': {'food':1324, 'var1': 2039, 'var2': 2126, 'var3': 0}},
{'app' : 'chrome',
'id': 'asdfb',
'name': 'Sam',
'selection': {'food':1324, 'var1': 2011, 'var2': 2123, 'var3': 0}},
{'app' : 'chrome',
'id': 'sfsf',
'name': 'Mary',
'selection': {'food':1324, 'var1': 2013, 'var2': 2111, 'var3': 0}}]

I want to only get a dictionary with a specific value. For example, in this example,

if MyDto['id'] = 'sfsf'

The result I want to get is

extracted_dict = {'app' : 'chrome',
'id': 'sfsf',
'name': 'Mary',
'selection': {'food':1324, 'var1': 2013, 'var2': 2111, 'var3': 0}

Is there a way to get a dictionary based on a specific value it includes? Sorry if this is too basic question :( Thanks a lot!

CodePudding user response:

I would go with something like:

key,value = 'id','sfsf'
dictList = [ myDict for myDict in MyDto if myDict.get(key) == value ]
print(dictList)

output

[{'app': 'chrome', 'id': 'sfsf', 'name': 'Mary', 'selection': {'food': 1324, 'var1': 2013, 'var2': 2111, 'var3': 0}}]

The list comprehension obviously produces a list of dicts and not a single dict. With this list comprehension you scan all the dicts in your MyDto, and select only the ones which contain the pair key/value you chose. It also works if the key is not present in any dictionary in MyDto, that is it avoids a KeyError exception and produces and empty list.

If you are sure that at most only one dict with key/value is contained in the list:

def getByKeyAndValue(dictList, key, value):
        for myDict in dictList:
            if myDict.get(key) == value:
                return myDict
        return {}

extracted_dict = getByKeyAndValue(MyDto, 'id', 'sfsf')

This function scans your MyDto only until it finds the dict you want to extract. If it does not find the dict, it returns an empty dict.

CodePudding user response:

Just iterate over items and get the right dict as below:

MyDto = [
    {'app': 'chrome',
     'id': 'adetwt',
     'name': 'John',
     'selection': {'food': 1324, 'var1': 2039, 'var2': 2126, 'var3': 0}},
    {'app': 'chrome',
     'id': 'asdfb',
     'name': 'Sam',
     'selection': {'food': 1324, 'var1': 2011, 'var2': 2123, 'var3': 0}},
    {'app': 'chrome',
     'id': 'sfsf',
     'name': 'Mary',
     'selection': {'food': 1324, 'var1': 2013, 'var2': 2111, 'var3': 0}}
]


def get_by_id(id: str):
    for dict_ in MyDto:
        if dict_['id'] == id:
            return dict_['selection']


print(get_by_id('sfsf'))

CodePudding user response:

gotta love list comprehension:

[d for d in MyDto if d['id'] == 'sfsf']
  •  Tags:  
  • Related