Multi-dimensional list initialization
Joshua Landau
joshua.landau.ws at gmail.com
Mon Nov 5 16:51:05 EST 2012
On 5 November 2012 06:27, Demian Brecht <demianbrecht at gmail.com> wrote:
> >>> a = [None] * 4
> >>> a[0] = 'a'
> >>> a
> ['a', None, None, None]
>
> >>> m = [[None] * 4] * 4
> >>> m[0][0] = 'm'
> >>> m
> [['m', None, None, None], ['m', None, None, None], ['m', None, None,
> None], ['m', None, None, None]]
>
> Is this expected behaviour and if so, why? In my mind either result makes
> sense, but the inconsistency is what throws me off.
z = [[None] * 4]
goes to
z = [x, x, x, x]
where x = [y]
where y = None
AND THEN
z[0] = 2
means
z = [p, x, x, x]
where p = 2
AND
z[1][0] = 3
means
x = [q]
where q = 3
hence z = [2, [3], [3], [3]]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121105/f0a47186/attachment.html>
More information about the Python-list
mailing list