problem with dictionaries within lists
Grant Hallam
gupsmail at ihatespam.shaw.ca
Wed Sep 25 11:02:06 EDT 2002
Thnks for the clarification...I must have gotten confused on that one.
:), I didn't realize that dict was a builtin...changed to be more
representative of the actual contents. Using the .copy() worked great.
Thanks.
Grant (da gup)
On Wed, 25 Sep 2002 07:51:21 -0700, Alex Martelli wrote:
> Grant Hallam wrote:
>
>> The problem with blanking out the dictionary with dict{} is that it
>> wipes the keys out. As the loop goes through it assigns the matching
>> second half of the list to a matching key. If you insert a print
>> statement on the dict you can see the values in the dict are changing.
>> Thus while it is the same dict the key values within the dict are
>> different. Thats why I'm somewhat confused as to why its the same dic
>> value. As for the tabs,
>
> It's the same dict object because you didn't copy it. You just appended
> many references to it to the list. So, just use:
>
>>> webdatabaselist.append(dict.copy())
>
> appending *copies* of the dict to the list, and you should be OK.
>
> BTW, avoid naming your own variables like builtins -- list, dict, file,
> int, str, &c -- you'll come to grief eventually when you do that and
> then some other piece of your code tries to use the builtin in the
> normal way. It's always possible to find a more significant name,
> anyway, so avoiding the types' names is no burden. (This has nothing to
> do with your specific problem -- it's just general advice).
>
>
> Alex
More information about the Python-list
mailing list