New submission from Darrell Long <darr...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue14719>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to