Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

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

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

May 3, 2012, 9:13 PM

Post #1 of 5 (51 views)
Permalink
[issue14719] Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)]

New submission from Darrell Long <darrell [at] cs>:

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>
<http://bugs.python.org/issue14719>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2012, 9:15 PM

Post #2 of 5 (51 views)
Permalink
[issue14719] Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)] [In reply to]

Changes by Darrell Long <darrell [at] cs>:


----------
type: -> behavior

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14719>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2012, 11:06 PM

Post #3 of 5 (53 views)
Permalink
[issue14719] Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)] [In reply to]

Yuval Greenfield <ubershmekel [at] gmail> 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>
<http://bugs.python.org/issue14719>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2012, 11:18 PM

Post #4 of 5 (48 views)
Permalink
[issue14719] Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)] [In reply to]

Martin v. Löwis <martin [at] v> added the comment:

It's actually fairly easy to explain. Just think about it harder (and consider Yuval's explanation).

----------
nosy: +loewis
resolution: -> invalid
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14719>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 3, 2012, 11:45 PM

Post #5 of 5 (50 views)
Permalink
[issue14719] Lists: [[0]*N]*N != [[0 for _ in range(N)] for __ in range(N)] [In reply to]

Ezio Melotti <ezio.melotti [at] gmail> added the comment:

http://docs.python.org/faq/programming.html#how-do-i-create-a-multidimensional-list
http://python.net/crew/mwh/hacks/objectthink.html

----------
nosy: +ezio.melotti
stage: -> committed/rejected

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14719>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.