[Tutor] dictionary append

Dinesh B Vadhia dineshbvadhia at hotmail.com
Thu Nov 1 20:30:53 CET 2007


Hello!  I'm creating a dictionary called keywords that has multiple entries each with a variable list of values eg.

keywords[1] = [1, 4, 6, 3]
keywords[2] = [67,2]
keywords[3] = [2, 8, 5, 66, 3, 23]
etc.

The keys and respective values (both are integers) are read in from a file.  For each key, the value is append'ed until the next key.  Here is the code.

.............
>>> keywords = {}
>>> with open("x.txt", "r") as f:
    k=0
    for line in f.readlines():
            keywords[k], second = map(int, line.split())
            keywords[k].append(second)
            if keywords[k] != k:
                    k=k+1
   
Traceback (most recent call last):
  File "<pyshell#44>", line 5, in <module>
    keywords[k].append(second)
AttributeError: 'int' object has no attribute 'append'
.............

Any idea why I get this error?

Dinesh


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20071101/774f25da/attachment.htm 


More information about the Tutor mailing list