trouble with lists

Kristian Zoerhoff kristian.zoerhoff at gmail.com
Tue May 3 13:54:29 EDT 2005


On 3 May 2005 10:42:43 -0700, anandr86 at gmail.com <anandr86 at gmail.com> wrote:
> I'm new to python. I tried doing this
> 
> >>> x = [[]] * 3
> >>> print x
> [ [] [] [] ]
> >>> x[0].append( 2 )
> [ [2] [2] [2] ]
> 
> I confused with the last line output. I actually expected something
> like
> 
> [ [2] [] [] ]

I think you want

>>> x[0] = [2]

But I have no idea why you're using nested lists in this case ([2] is
a one-item-long list, so x is actually a list of lists here). Are you
sure this is really what you want?

-- 
Kristian

kristian.zoerhoff(AT)gmail.com
zoerhoff(AT)freeshell.org



More information about the Python-list mailing list