problem with dictionaries within lists

Alex Martelli aleax at
Wed Sep 25 10:51:21 EDT 2002

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).


More information about the Python-list mailing list