Stack Exchange
How to add particular element of dict
sleeps=[{"summaryId ": "x209bf59-59b0c5a8-585c ", 
         "validation ": "AUTO_TENTATIVE ", 
         "durationInSeconds ": 22620, 
         "startTimeInSeconds ": 1504757160, 
         "awakeDurationInSeconds ": 900, 
         "startTimeOffsetInSeconds ": -14400, 
         "deepSleepDurationInSeconds ": 8160, 
         "lightSleepDurationInSeconds ": 13560},
        {"summaryId ": "x209bf59-59b0c5a8-23a0 ", 
         "validation ": "AUTO_TENTATIVE ",
         "durationInSeconds ": 9120, 
         "startTimeInSeconds ": 1504757160,
         "awakeDurationInSeconds ": 300, 
         "startTimeOffsetInSeconds ": -14400, 
         "deepSleepDurationInSeconds ": 1680, 
         "lightSleepDurationInSeconds ": 7140}
       ]

res = sum([x["durationInSeconds"] for x in sleeps]) 
print(res)

This is throwing a Keyerror.

The key you want to modify has a redundant space in your dict definition
"durationInSeconds "
                  ^


Hashing and comparisons take whitespaces into account. Probably easier to spot the difference if they are printed next to each other:
"durationInSeconds "
"durationInSeconds"

The first one has a trailing whitespace.
>>> hash(s1) == hash(s2)
False
>>> s1 == s2
False

So they are in no respect equal to each other which also explains the KeyError. To fix it you either need to remove the trailing whitespace in the dictionary, or look up the key with the trailing whitespace. For example:
>>> res = sum([x["durationInSeconds "] for x in sleeps]) 
>>> print(res)
31740

