Nested dictionary comprehension python

{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.

Leave a Comment