简体   繁体   中英

Pythonic way to retrieve single level from nested dictionary

I am trying to retrieve a single level from a nested dictionary. So for instance, given the dictionary below, I would expect to see the following results for the first and second level.

nested_dict = {
  'foo':'bar',
  'baz':{
    'foo':'baz'
  }}

# level 0
{'foo':'bar'}

# level 1
{'foo':'baz'}

I can retrieve the first level using a dict comprehension:

{k:v for (k,v) in nested_dict.items() if type(v) is not dict}
>>> {'foo':'bar'}

Or retrieve a specified level using a recursion:

def get_level(nested_dict, level):
  if level == 0:
    return {k:v for (k,v) in nested_dict.items() if type(v) is not dict}
  else:
    this_level = {}
    for (k,v) in nested_dict.items():
      if type(v) is dict:
        this_level.update(v)
    return get_level(this_level, level - 1)

get_level(nested_dict, 1)
>>> {'foo':'baz'}

I am now wondering if there is a more Pythonic/clean/out of the box way to retrieve the levels of nested dictionaries (as I already did above), if necessary with help of a package.

As stated in the comments, I'm assuming you don't have duplicates:

nested_dict = {
  'foo':'bar',
  'baz':{
    'foo':'baz',
    'fee': {
        'foo': 'fee'
    }
  },
  'baz2':{
    'foo2':'baz2'
  }
  }

def get_level(dct, level):
    if level == 0:
        yield from ((k, v) for k, v in dct.items() if not isinstance(v, dict))
    else:
        yield from ((kk, vv) for v in dct.values() if isinstance(v, dict) for kk, vv in get_level(v, level-1))


print(dict(get_level(nested_dict, 1)))

Prints:

{'foo': 'baz', 'foo2': 'baz2'}

print(dict(get_level(nested_dict, 2)))

Prints:

{'foo': 'fee'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM