Extract all keys in the second level of nested dictionary

Viewed 6079

I would like to extract all keys in second level of 2d dictionary but python interpreter return NameError. my expected result is ['aa', 'bb', 'cc', 'aaa', 'bbb', 'ccc']

>>> adict
defaultdict(<class 'dict'>, {'b': {'aaa': 444, 'ccc': 666, 'bbb': 555}, 'a': {'aa': 111, 'cc': 333, 'bb': 222}})

>>> all = [ele for ele in adict[ww].keys() for ww in ['a', 'b']]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ww' is not defined
2 Answers
Related