list of dictionaries

Remco Gerlich scarblac at
Thu Jun 28 04:06:34 EDT 2001

George Thomas <george at> wrote in comp.lang.python:
> That makes sense. However, to make sure I understand completely, I'll
> list the steps in question.
> Given: A single dict object, call it dict (how original!)
> 1. Start with empty list
> 2. Update fields of dict
> 3. append dict to list.
> 4. Update fields of dict.
> 5. append dict to list.
> The thing I can't understand is: even though dict reflects the change in
> (4), the append() operation seems to ignore it completely, giving me a
> copy of the previous contents. Why does this happen ?

You append the same dictionary every time. There is no copy. The list just
stores references to objects - all elements of the list are just references
to the same dictionary. 

When you do dict_entry = {} in between, you make a new dictionary, fill
that, then add it to the list, so that there are different dicts in the list.

Alternatively, you could use 
to explicitly make a copy of the dictionary (but I think you want a new one
each time, conceptually). (Oh, Emile already said that).

Lesson learned: all variables are just references in Python, it never makes
a copy implicitly, everything is done by handing references to objects around.

Remco Gerlich

More information about the Python-list mailing list