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: making assignments to an empty two dimensional list
Type: behavior Stage:
Components: Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: drathlian, mark.dickinson
Priority: normal Keywords:

Created on 2012-02-02 00:40 by drathlian, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
twoDimensionalList.py drathlian, 2012-02-02 00:40
Messages (2)
msg152450 - (view) Author: Dave E (drathlian) Date: 2012-02-02 00:40
I might be missing something, but I am expecting the following code to print out a list of lists with each internal list holding one number[0-4], but instead the internal lists are a copy of the list "count".  

#!/usr/bin/python
count = range(4)
twoDimensionList = [[]] * len(count)
for i in range(len(count)):
   twoDimensionList[i].append(count[i])
print twoDimensionList

Should print: 
[[0], [1], [2], [3]]

but erroneously prints:
[[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]
msg152451 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2012-02-02 00:47
This isn't a bug, but a known gotcha:  see.

http://docs.python.org/faq/programming.html#how-do-i-create-a-multidimensional-list
History
Date User Action Args
2022-04-11 14:57:26adminsetgithub: 58133
2012-02-02 00:47:08mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg152451

resolution: not a bug
2012-02-02 00:40:48drathliancreate