Check nested dictionary values?

前端 未结 3 961
别跟我提以往
别跟我提以往 2020-12-28 09:38

For a large list of nested dictionaries, I want to check if they contain or not a key. Each of them may or may not have one of the nested dictionaries, so if I loop this sea

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 10:31

    How about a try/except block:

    for Dict1 in DictionariesList:
        try:
            if 'Dict4' in Dict1['Dict2']['Dict3']:
                print 'Yes'
        except KeyError:
            continue # I just chose to continue.  You can do anything here though
    

提交回复
热议问题