dictionary containing a list

goyatlah
Sat Oct 7 06:53:22 CEST 2006


I think what you mean is that if you change your list, it is changed
somewhere in your dicrionary to. Lists are always copied as pointers,
except explicitly told other wise. So a = b = [] makes a and be the
same list, and a.append(1) makes b ->  [1].
So do something like mydict[mykey] = mylist[:] (Slicing gives a copy
of the list, not the pointer).
Hope this helps.

Moi
Dolf

On 6 Oct 2006 14:37:59 -0700, "Ben" <Benjamin.Barker at gmail.com> wrote:

>Hello...
>
>I have set up a dictionary into whose values I am putting a list. I
>loop around and around filling my list each time with new values, then
>dumping this list into the dictionary. Or so I thought...
>
>It would appear that what I am dumping into the dictionary value is
>only a pointer to the original list, so after all my iterations all I
>have is a dictionary whose every value is equal to that of the list the
>final time I looped around :-(
>
>Is there a way to acheive what I was attempting ? I have done something
>almost identical with classes  in a list before, and in that case a new
>instance was created for each list entry...
>
>
>I hope this makes some sense, and doesn't seem to head bangingly
>simple...
>
>
>Cheers,
>
>Ben




More information about the Python-list mailing list