Home > Mobile >  Extract fixed position values in nested dictionary Python
Extract fixed position values in nested dictionary Python

Time:05-19

d = {'k1':{'inside1':[1,'one']},
     'k2':{'inside2':[2,'two']},
     'k3':{'inside3':[3,'three']}}

possible to get the expected result of 5 (1 2 3) using for loop?

CodePudding user response:

You can do:

sum((v2[0] for v in d.values() for v2 in v.values()))
6

CodePudding user response:

result = sum([sum([lst[0] for lst in dic.values()]) for dic in d.values()])
  • Related