Home > Mobile >  pythonic way to get the values from each of the last level of nested dictionary
pythonic way to get the values from each of the last level of nested dictionary

Time:03-05

I have the following dict:

x = {'NIH': {'slide_1': {'ROI1': [1], 'ROI2': [2]}}}

And I want to get the last nested level dict, I'm currently doing the following:

x = list(list(list(cc.values())[0].values())[0])

I was wondering if there is a more elegant way of achieving the same results. There are more levels to the nested dict but for the sake of the question this example should suffice.

CodePudding user response:

This is a generic way of getting the last nested level dict even for variable levels of nesting:

        x = {'NIH': {'slide_1': {'ROI1': [1], 'ROI2': [2]}}}      
        while isinstance(y := list(x.values())[0], dict):
            x = y
        print(x)

Output:

{'ROI1': [1], 'ROI2': [2]}

CodePudding user response:

I am not sure if there is a pythonic way, but you can use a recursive function:

cc = {'NIH': {'slide_1': {'ROI1': [1], 'ROI2': [2]}}}

def getLatestLevel(dictionary, values=[]):
    for key, item in dictionary.items():
        if type(item) is dict:
            values = getLatestLevel(item, values)
        else:
            values.append(key)
    return values
            
x = getLatestLevel(cc)

Output of print(x):

['ROI1', 'ROI2']

It returns the same as your proposed line but is general. Replace key by item if you want to get the values instead of the keys.

Note: This will collect all the entries from each of the last levels (leafs) of the dictionary tree even if some nested dictionaries which are not the leafs contain multiple entries. If dictionaries along the path are known to only contain one entry, then @constantstranger solution is certainly simpler.

  • Related