beginner question about range with list and list

erik gartz eegunnar at yahoo.com
Thu Nov 23 23:07:11 EST 2006


Hello,
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? It almost appears as if the 2nd dictionary
created overwrites the first one. Thanks for your help,
Erik

>>>
>>> a = [[{}] *  3] * 2
>>> a
[[{}, {}, {}], [{}, {}, {}]]
>>> for i in range(2):
	a[i][1] = {'x':i}
>>> a
[[{}, {'x': 1}, {}], [{}, {'x': 1}, {}]]
>>>




More information about the Python-list mailing list