Dictionary with List of Values
<p>I have a file such as:</p>

<pre><code>a 1
a 2
b 5
c 8
a 9
</code></pre>

<p>I want to add together the second field per key so that I have an aggregate number and therefore a single key:value pair.</p>

<p>With a large dataset I am thinking the best way to go about this would be to create a dictionary that contains a list of values per unique key. Is this the best approach?</p>

<p>How do I set the lists of values per key accurately (below code seems to overwrite values instead of appending)?</p>

<pre><code>dict={}
file=open('foo.txt','r')
lines=file.readlines()
for line in lines:
        k, v=line.split()
        dict[k]=[v]
</code></pre>

<p>now if i want to take the aggregate numbers populated in first dictionary and compare both the keys and the values against keys and values in another dictionary to determine differences between the two, i can only conclude something like the following:</p>

<p>for i in res.keys():</p>

<pre><code>if res2.get(i):
    print 'match',i
else:
    print i,'does not match'
</code></pre>

<p>for i in res2.keys():</p>

<pre><code>if res.get(i):
    print 'match',i
else:
    print i,'does not match'
</code></pre>

<p>for i in res.values():</p>

<pre><code>if res2.get(i):
    print 'match',i
else:
    print i,'does not match'
</code></pre>

<p>for i in res2.values():</p>

<pre><code>if res.get(i):
    print 'match',i
else:
    print i,'does not match'
</code></pre>

<p>cumbersome and buggy...need help!</p>
