Search code examples
pythonpython-3.xdictionarynested-loopsdictionary-comprehension

3 level nested dictionary comprehension in Python


I have a Python dictionary as follows:

d = {'1': {'1': 3, '2': 1, '3': 1, '4': 4, '5': 2, '6': 3},
     '2': {'1': 3, '2': 3, '3': 1, '4': 2},
     '3': {'1': 1, '2': 1, '3': 3, '4': 2, '5': 1, '6': 1, '7': 1},
     '4': {'1': 1, '2': 1, '3': 3, '4': 2, '5': 1, '6': 1, '7': 1}}

I have this operation on the dictionary:

D = {}
for ko, vo in d.items():
  for ki, vi in vo.items():
    for i in range(vi):
      D[f'{ko}_{ki}_{i}'] = someFunc(ko, ki, i)

I want to translate it into a one liner with dictionary comprehension as follows:

D = {f'{ko}_{ki}_{i}': someFunc(ko, ki, i) for i in range(vi) for ki, vi in vo.items() for ko, vo in d.items()}

But I get an error

NameError: name 'vi' is not defined

Can someone help me with the correct syntax for achieving this?


Solution

  • The order of the loops has to be reversed.

    This is what you're looking for:

    D = {f'{ko}_{ki}_{i}': someFunc(ko, ki, i) for ko, vo in d.items() for ki, vi in vo.items() for i in range(vi)  }