[Tutor] Question if my reasoning is right
Darth Kaboda
darthkaboda at msn.com
Wed Aug 19 07:52:50 CEST 2009
I'm questioning my reason for why the follwoing code doesn't behave as I thought it would upon first coding a project.
m = 8
n = 10
cb = [[[0, None]] * (n + 1)] * (m + 1)
cb[3][2][0] = 10
This last statement causes the every first element in the list to update. Is this becuase this method of initializing a list is just a copy of the same list in memory?
To get around this I'm now doing the folowing:
m = 8
n = 10
cb = [[[0,None] for x in range(n+1)] for y in range(m+1)]
cb[3][2][0] = 10
This is working by updating only the one list as I want it to. Is this an acceptable practice? Or should I use the standard nested for loops like you'd do in most other languages?
Any insight would be greatly appreciated.
Thanks,
Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090818/37f3577a/attachment.htm>
More information about the Tutor
mailing list