Q:

Stumped - In Python obtaining unique keys with multiple associated values from a list of ditionaries

I'm parsing a csv file to perform some basic data processing. The file that I am working with is a log of user activity to a website formatted as follows:
User ID, Url, Number of Page Loads, Number of Interactions 

User ID and Url are strings, Number of Page Loads and Number of Interactions are integers.
I am attempting to determine which url has the best interaction-to-page ratio.
The part I am struggling with is getting unique values and aggregating the results from the columns.
I've written the following code:
import csv
from collections import defaultdict

fields = ["USER","URL","LOADS","ACT"]

file = csv.DictReader(open('file.csv', 'rU'), delimiter=",",fieldnames=fields)
file.next()

dict = defaultdict(int)

for i in dict:
    dict[i['URL']] += int(i['LOADS'])

This works fine. It returns a list of unique urls with the number of total loads by url in a dictionary - { 'URL A' : 1000 , 'URL B' : 500}
The issue is when i try to add multiple values to the url key, I'm stumped.
I've tried amending the for loop to do:
for i in dict:
    dict[i['URL']] += int(i['LOADS']), int(i['ACT'])

and I receive TypeError: unsupported operand type(s) for +=: 'int' and 'tuple'. Why is the second value considered a tuple?
I tried adding just int(i[ACT]), and it worked fine. It's just when I try both values at the same time.  
I'm on python 2.6.7; Any ideas on how to do this and why it's considered a tuple?

A:

You are better off using a list as your defaultdict container:
import csv
from collections import defaultdict

d = defaultdict(list)
fields = ["USER","URL","LOADS","ACT"]

with open('file.csv', 'rU') as the_file:
    rows = csv.DictReader(the_file, delimiter=",",fieldnames=fields)
    rows.next()

    for row in rows:
        data = (int(row['LOADS']),int(row['ACT']))
        d[row['URL']].append(data)

Now you have
d['someurl'] = [(5,17),(7,14)]

Now you can do whatever sums you would like, for example, all the loads for a URL:
load_sums = {k:sum(i[0] for i in d[k]) for k in d}

