List assignment, unexpected result
Dave Reed
dreed at capital.edu
Mon Jul 8 17:03:57 EDT 2002
> From: Paul Rubin <phr-n2002b at NOSPAMnightsong.com>
<snip>
> > The intent is clear i.e. fill the diagonal with 0,1,2,3; but the
> > result is somewhat different. Could anyone explain why this doesn't
> > work as expected - and even better, come up with an assignment for
> > 'grid' that would work.
>
> grid = [['.' * 4] for i in range(4)]
>
> is one way.
I think:
grid = [['.',] * 4 for i in range(4)]
is what you need otherwise you get:
[['....'], ['....'], ['....'], ['....']]
and grid[1][1] gives you and index error
Dave
More information about the Python-list
mailing list