beginner question about range with list and list

Fredrik Lundh fredrik at pythonware.com
Fri Nov 24 01:44:36 EST 2006


erik gartz wrote:

> I'm new to python and I'm having difficulty understanding the following
> code. Why doesn't the variable a contain  [[{}, {'x': 0}, {}], [{},
> {'x': 1}, {}]] instead. Doesn't {} allocate new memory for the
> dictionary each time?

each time it's *executed*, yes.  [{}]*3 doesn't execute [] or {} three 
times; it executes them both once, and then creates a new list with 
three references to the dictionary.  see:

http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm

</F>




More information about the Python-list mailing list