[issue14719] Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)]

Darrell Long report at bugs.python.org
Fri May 4 06:13:42 CEST 2012


New submission from Darrell Long <darrell at cs.ucsc.edu>:

N = 5
board_1 = [[0 for _ in range(N)] for __ in range(N)]

is not the same as:

board_2= [[0]*N]*N

One makes a proper list of lists (the first), the second makes a new kind of animal were board_2[1][1] = 99 changes a whole column.

Oddly, testing board_1 == board_2 is True!

I'm teaching Python to non-majors, and this is a tough one to explain.

----------
components: Interpreter Core
files: Screen Shot 2012-05-03 at 9.05.59 PM.png
messages: 159899
nosy: darrell
priority: normal
severity: normal
status: open
title: Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)]
versions: Python 2.7
Added file: http://bugs.python.org/file25450/Screen Shot 2012-05-03 at 9.05.59 PM.png

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


More information about the Python-bugs-list mailing list