Sum values in dictionary
<p>I'm working with an Excel file and openpyxl.</p>

<p>Below is sample data:</p>

<pre><code>Name    Value
Amy1    4
Bob1    5
Bob1    5
Bob2    8
Chris1  7
Chris2  3
Chris3  6
Chris3  6
Chris3  6
</code></pre>

<p>Using the for loop below, I grab the value associated with each unique name.</p>

<pre><code>for rowNum in range(2, 11):
    person = sheet.cell(row = rowNum, column = 13).value
    people.append(person)
    personValue.update({person: sheet.cell(row = rowNum, column = 26).value})
</code></pre>

<p>That yields a dictionary with a single entry for each name (Amy1, Bob1, Bob2, etc.).</p>

<p>I want to merge and sum the value for each matching name to return the following result:</p>

<pre><code>Name  Value
Amy   4
Bob   13
Chris 16
</code></pre>

<p>If <code>name = sheet.cell(row = rowNum, column = 13).value</code></p>

<p>And <code>value =  sheet.cell(row = rowNum, column = 26).value</code></p>

<p>Edited according to your comments:</p>

<pre><code>from collections import defaultdict

people = defaultdict(int)
category = defaultdict(int)

for rowNum in range(2, 11):
    # Name with number
    person = sheet.cell(row = rowNum, column = 13).value
    # Name without number
    category_name = ''.join([c for c in person if not c.isdigit()])
    people[person] += sheet.cell(row = rowNum, column = 26).value
    category[category_name] += sheet.cell(row = rowNum, column = 26).value
</code></pre>

<p>Result - people:</p>

<pre><code>Name    Value
Amy1    4
Bob1    10
Bob2    8
Chris1  7
Chris2  3
Chris3  18
</code></pre>

<p>Result - category:</p>

<pre><code>Name    Value
Amy     4
Bob     18
Chris   28
</code></pre>

<p>This will work.</p>

<p>Thanks Padraic Cunningham.
I just realized what i misunderstood.</p>

python list dictionary sum