adding multiple new values to the same key in a dictionary

Krishna Pacifici pacificik at warnell.uga.edu
Tue Aug 11 15:47:47 EDT 2009


Hi,
I want to be able to add multiple new values to a key in a dictionary.

I have tried the following:

sec_dict_clean=
{88: [87, 89, 78, 98], 58: [57, 59, 48, 68], 69: [79], 95: [94, 96, 85]}

for i in range(len(sec_dict_clean.values())):
    for j in range(len(sec_dict_clean.values()[i])):
        sec_dict_clean.setdefault(key,[]).append(blocks[sec_dict_clean.values()[i][j]].abundance)

where blocks[...].abundance gives me a single value from an object,

but this gives me the following:

sec_dict_clean=
{88: [87, 89, 78, 98], 58: [57, 59, 48, 68], 69: [79], 95: [94, 96, 85, 4, 12, 11, 6, 9, 12, 11, 7, 10, 10, 12, 9, 6, 12, 15, 9, 8, 12, 15, 12, 12]}

instead I want each abundance (starts with 4, 12...) to be associated with each of the values so that it would look like this:

sec_dict_clean=
{88: ([87, 89, 78, 98], [4,12,11,6]), 58: ([57, 59, 48, 68], [9,12,11,7]), 69: ([79], [10])...}

You can see there are several errors here (I have more things being appended than there are values in the dictionary), but I really just want to know how to add multiple values to the same key in a dictionary.

Thanks for any help,
Krishna


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090811/46939da6/attachment.html>


More information about the Python-list mailing list