This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: list of list created by *
Type: behavior Stage:
Components: None Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: flox, sledge76
Priority: normal Keywords:

Created on 2010-02-12 14:49 by sledge76, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg99266 - (view) Author: Andrew Chong (sledge76) Date: 2010-02-12 14:52
This shows unexpected behavior.

>>> data2 = [[]] * 4
>>> print data2
[[], [], [], []]
>>> data2[0] += [(0,1)]
>>> print data2
[[(0, 1)], [(0, 1)], [(0, 1)], [(0, 1)]]

I added a tuple to only 0th list, but it got added to all the lists in the global list.
msg99267 - (view) Author: Andrew Chong (sledge76) Date: 2010-02-12 14:55
But this works fine.

>>> data = []
>>> data += [[]]
>>> data += [[]]
>>> data += [[]]
>>> data += [[]]
>>> print data  
[[], [], [], []]
>>> data[0] += [(0,1)]
>>> print data        
[[(0, 1)], [], [], []]
msg99268 - (view) Author: Florent Xicluna (flox) * (Python committer) Date: 2010-02-12 14:56
It is by design.

Please read the documentation:
http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange
History
Date User Action Args
2022-04-11 14:56:57adminsetgithub: 52165
2010-02-12 14:56:24floxsetstatus: open -> closed
2010-02-12 14:56:11floxsetresolution: not a bug

messages: + msg99268
nosy: + flox
2010-02-12 14:55:42sledge76setmessages: + msg99267
2010-02-12 14:52:32sledge76settype: behavior
messages: + msg99266
components: + None
2010-02-12 14:49:46sledge76create