[Tutor] Question if my reasoning is right
Alan Gauld
alan.gauld at btinternet.com
Wed Aug 19 11:34:30 CEST 2009
"Darth Kaboda" <darthkaboda at msn.com> wrote
> 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
Yes exactly.
> To get around this I'm now doing the folowing:
> cb = [[[0,None] for x in range(n+1)] for y in range(m+1)]
Yes, that's what list comprehensions are for, to create lists.
> Is this an acceptable practice?
Absolutely. It is one of the most common uses for comprehensions.
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list