[issue13925] making assignments to an empty two dimensional list

Dave E report at bugs.python.org
Thu Feb 2 01:40:49 CET 2012


New submission from Dave E <david.elzinga at gmail.com>:

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]]

----------
files: twoDimensionalList.py
messages: 152450
nosy: drathlian
priority: normal
severity: normal
status: open
title: making assignments to an empty two dimensional list
type: behavior
versions: Python 2.7
Added file: http://bugs.python.org/file24390/twoDimensionalList.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13925>
_______________________________________


More information about the Python-bugs-list mailing list