Home > Software design >  Python: sum of specific values of a list inside a dictionary
Python: sum of specific values of a list inside a dictionary

Time:09-29

I have this dictionary with a list of values on it:

d = {'1749': [58.0, 62.6, 70.0, 55.7, 85.0] 
 '1750': [73.3, 75.9, 89.2, 88.3, 90.0] 
 '1751': [70.0, 43.5, 45.3, 56.4, 60.7]}

I wish to sum the last two values from 1749, all the values from 1750, and the first two values of 1751 altogether.

CodePudding user response:

Do you mean by?

>>> sum(d['1749'][-2:]   d['1750']   d['1751'][:2])
670.9
>>> 

Or:

>>> sum(sum(d.values(), [])[3:-3])
670.9
>>> 
  • Related