I'm having trouble understanding nested dictionary comprehensions in Python 3. The result I'm getting from the example below outputs the correct structure without error, but only includes one of the inner key: value pairs. I haven't found an example of a nested dictionary comprehension like this; Googling "nested dictionary comprehension python" shows legacy examples, non-nested comprehensions, or answers solved using a different approach. I may be using the wrong syntax.
Example:
data = {outer_k: {inner_k: myfunc(inner_v)} for outer_k, outer_v in outer_dict.items() for inner_k, inner_v in outer_v.items()}
This example should return the original dictionary, but with the inner value modified by myfunc
.
Structure of the outer_dict dictionary, as well as the result:
{outer_k: {inner_k: inner_v, ...}, ...}
{inner_k: myfunc(inner_v)}
isn't a dictionary comprehension. It's just a dictionary.
You're probably looking for something like this instead:
data = {outer_k: {inner_k: myfunc(inner_v) for inner_k, inner_v in outer_v.items()} for outer_k, outer_v in outer_dict.items()}
For the sake of readability, don't nest dictionary comprehensions and list comprehensions too much.