[Tutor] dictionary of lists

Chris Stinemetz chrisstinemetz at gmail.com
Wed Jun 3 18:39:34 CEST 2015


I am trying to create a dictionary of lists as I read a file. I
envision it looking like: {key: [float_type],[string_type]}

For the first item in the list I am trying to add the value to the
existing value where the key matches but I am getting the following
error:

Resetting execution engine
Running C:\Users\cs062x\Desktop\python\projects\PanHandle\PanHandle\PanHandle.py
The Python REPL process has exited
Traceback (most recent call last):
  File "C:\Users\cs062x\Desktop\python\projects\PanHandle\PanHandle\PanHandle.py",
line 22, in <module>
    d[IMEI] += Elapsed_Mins
TypeError: 'float' object is not iterable

Here is the code. My question is how can I keep it list type and just
sumup the first element in the list as I read it?

d = defaultdict(list)
for fname in os.listdir('.'):
    with open (fname) as csvfile:
        spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
        next(spamreader)
        for row in spamreader:

            if row[8]:
                device = row[36]
                Elapsed_Mins = float(row[7])
                IMEI = row[8].replace("'", "")

                d[IMEI] += Elapsed_Mins ## this is where the error occurs.

Thanks in advance,

Chris


More information about the Tutor mailing list