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

Yuval Greenfield report at bugs.python.org
Fri May 4 08:06:20 CEST 2012


Yuval Greenfield <ubershmekel at gmail.com> added the comment:

This isn't a bug and should be closed. It's more of a stack overflow question.

If you'd like to change this fundamental behavior of a very common operation in python you should make a proposal to the python ideas mailing list at http://mail.python.org/mailman/listinfo/python-ideas

In your example board_2 is equivalent to:

    row = [0] * N
    board_2 = row * N

All the rows are the same initial row. As opposed to board_1 where each row is a new row.

Try this:

    [id(i) for i in board_2]

The initial equivalence is because they do represent the same values (NxN list of all zeroes). What should python compare if not by values?

----------
nosy: +ubershmekel

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


More information about the Python-bugs-list mailing list