Martin Panter added the comment:
This is how Python is meant to work; see
https://docs.python.org/2.7/faq/programming.html#how-do-i-create-a-multidimensional-list.
Unless there is something in the documentation that gave you the wrong
impression, I suggest we close this.
--
nosy:
Changes by Martin Panter vadmium...@gmail.com:
--
stage: - resolved
status: open - closed
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue24589
___
Zorceta added the comment:
FYI:
ll = [[]]*10
[id(l) for l in ll]
[67940296, 67940296, 67940296, 67940296, 67940296, 67940296, 67940296,
67940296, 67940296, 67940296]
--
nosy: +zorceta
___
Python tracker rep...@bugs.python.org
New submission from Jos Dechamps:
After creating a list of lists, changing one element, leads to changes of all
the elements:
v=[[]]*10
v
[[], [], [], [], [], [], [], [], [], []]
v[3].append(3)
v
[[3], [3], [3], [3], [3], [3], [3], [3], [3], [3]]
v=[[]]*10
v
[[], [], [], [], [], [],
Bastiaan Albarda added the comment:
The * operator lets you use the same object multiple times, thereby saving
resources.
If you want unique objects use comprehension:
v = [[] for x in range(10)]
v[3].append(3)
v
[[], [], [], [3], [], [], [], [], [], []]
v[3] += [3]
v
[[], [], [], [3,