Simple list.append() question

Bjorn Pettersen bjorn at roguewave.com
Mon Apr 24 18:00:34 CEST 2000


Jeff Massung wrote:
> 
> I'm just learning, but my guess is this:
> 
> >>> l=[[]]*3
> >>> l
> [[], [], []]
> >>> l[0].append(3)
> >>> l
> [[3], [3], [3]]
> >>> id(l[0])
> 8539088
> >>> id(l[1])
> 8539088
> >>> id(l[2])
> 8539088
> >>>
> 
> The *3 just made three lists pointing to the same place. I don't know how to
> get what you want tho ;) someone else can enlighten me, too :).

One way is:

	x = map(lambda x:[], [[]]*3)

although there has to be a prettier way...

-- bjorn




More information about the Python-list mailing list