Possible Duplicate:
Unexpected feature in a Python list of lists
Python list confusion
Consider the following code:
a = [[]] * 3
a[1].append("foo")
I would expect the value of a
to become:
[[], ["foo"], []]
instead, every element of a
is updated:
[["foo"], ["foo"], ["foo"]]
Can someone please explain why did each element of the list got updated instead of just the defined element (a[1]
)? Is there something fundamentally wrong in my logic?
For what it's worth I'm running Python2.7