List Question

Scherer, Bill Bill.Scherer at VerizonWireless.com
Fri Jul 20 13:37:53 EDT 2001


>>> a=[[0]*6]*6
>>> a[3][3]=4
>>> a[0] is a[1] is a[2] is a[3] is a[4] is a[5]
1

This last line shows the source of your problem.  Each list subelement of
a is in fact the same list.

Build your list of lists so that each subelement is a different list, and
then a[3][3] = 4 will do what you want.



On Fri, 20 Jul 2001, Donovan Hide wrote:

> Given:
>
> a=[[0]*6]*6
> a[3][3]=4
> print a
>
> [[0, 0, 0, 4, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 4, 0,
> 0], [0, 0, 0, 4, 0, 0], [0, 0, 0, 4, 0, 0]]
>
> What syntax should I use to get this result:
>
> [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 4, 0,
> 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]]
>
> Thanks for the help,
> Donovan Hide.
>
>
>

William K. Scherer
Sr. Member of Applications Staff - Verizon Wireless
Bill.Scherer_at_VerizonWireless.com





More information about the Python-list mailing list