# Summing Values for Each Item in a Dictionary

```# Create dictionary with segmentid as key, calculated length as value1, measured length as value2
fiberCableLengths = defaultdict(list)
with arcpy.da.SearchCursor(fiberCable, ['segmentid', 'calculatedlength', 'measuredlength']) as cursor:
for row in cursor:
fiberCableLengths[row[0]].append(row[1])
fiberCableLengths[row[0]].append(row[2])

# Create slackloop dictionary with segmentidfkey as key, total up the length values of each slackloop under that segmentidfkey, and store as the dictionary value
slackloopLengths = defaultdict(list)
with arcpy.da.SearchCursor(slackLoop, ['segmentidfkey', 'length']) as cursor:
for row in cursor:
if row[1] is not None:
slackloopLengths[row[0]].append(int(row[1]))

sumValueDict = {k: sum(v) for k, v in slackloopLengths.items()}

# For each fibercable segmentid in dictionary, If calculated length + total slackloop length = measured length – Add to good list, Else – Add to bad list
goodList = []
with arcpy.da.SearchCursor(fiberCable, ['segmentid', 'calculatedlength', 'measuredlength']) as cursor:
for row in cursor:
for k, v in sumValueDict.items():
if row[0] == k:
if row[1] and row[2] is not None:
if int(row[1]) + int(v) == int(row[2]):
goodList.append(str(row[0]))
print "Good List: " + row[0]
print int(row[1]) + int(v)
print int(row[2])
else:
print "Bad List: " + row[0]
print int(row[1]) + int(v)
print int(row[2])
print "Good List Size: " + str(len(goodList))

The code above creates a dictionary where each item looks like the following: `'1f169eae-5d7c-4ebb-90cd-abbe0e1b72dd': [50.0, 50.0, 50.0, 50.0]`

How do I get it to sum the values for each key? The output I'd be looking for here would be: `4ebb-90cd-abbe0e1b72dd': [200]`

Resources I've looked at:

• What is your next step? You could use summary statistics
– Bera
Commented Jun 24, 2019 at 15:56

Iterate over the dict items and construct a new dict from the key and sum of the values:

``````>>> slackloopLength = {'abc': [1,2,4,7], 'def': [1,3,5,7,9]}
>>> {k: sum(v) for k, v in slackloopLength.items()}
{'abc': 14, 'def': 25}
>>>
``````

The way to read this construction is:

We're going to make a dict:

``````   {
``````

and each element will have key `k` and value `sum(v)`:

``````   {k: sum(v)
``````

where `k` and `v` are each of the key-value pairs of elements of `slackloopLength`:

``````   {k: sum(v) for k, v in slackloopLength.items()}
``````
• Would you mind taking that out of list comprehension format? I've yet to wrap my mind around that format. Commented Jun 24, 2019 at 15:23
• How else would you like it? That's probably the most "pythonic" way of doing this and wrapping it in a "for" loop would be inefficient and bad style. I've spelt it out now in the way I read these things. Commented Jun 24, 2019 at 15:52
• The code above creates a temporary output that sums the values of the original slackloopLengths dictionary container? How would I get it to update the values in my original dictionary? Commented Jun 24, 2019 at 16:30
• Reassign it: `slackloopLength = {blah blah}` Commented Jun 24, 2019 at 17:33