StealthPython StealthPython - 8 months ago 75
Python Question

Accessing keys of a dictionary within dictionary within dictionary

I got a few functions defined to set up some dictionaries and organize things. What I'm trying to do now is use a nested dictionary as a function parameter for another function.

K so I have this inputted format:

list = {'123': {'Name': 'foo', 'Responses': {'Q4': 'a', 'Q1': 'a', 'Q2': 'b', 'Q3': 'c'}, 'ID': '123'}, '234': {'Name': 'bar', 'Responses': {'Q4': 'b', 'Q1': 'a', 'Q2': 'b', 'Q3': 'c'}, 'ID': '234'}, '345': {'Name': 'xyzzy', 'Responses': {'Q4': 'b', 'Q1': 'a', 'Q2': 'a', 'Q3': 'c'}, 'ID': '345'}}

I need to access the 'Responses' key so that the function can use the Q1, Q2, etc keys and compare to another dictionart. And i need to do this for each 123, 234, 345 keys.
Then not so significant i have to map a key key for the values that function gives and update a list but I can figure that out once i get this function to run.

I've tried using list.get('Responses') as the parameter but it returns None so I'm not really sure where to go from here.


The .get() parameter looks for top level keys. What you actually probably want is to list over the sub-level dictionaries:

for key, sub_dict in toplevel_dict.items():
    responses = sub_dict['Responses']
    # do something with responses here; responses is a dict