creating lists question?
Steve
dippyd at yahoo.com.au
Thu Mar 25 01:19:52 EST 2004
Gandalf wrote:
> The * operator applied to a tuple will create another tuple consisting
> of the same objects but many times. For example,
> (2,3,) *3
> will create (2,3,2,3,2,3)
>
> However, if you do this:
>
> (obj,)*3
>
> then you will get
>
> (obj,obj,obj,)
>
> You will get references to the the same object. :-)
Which leads me to this nifty method:
a,b,c,d = map(lambda L: L[:], [[]]*4)
Or as a list comprehension:
a,b,c,d = [L[:] for L in [[]]*4]
It works, but it is moderately cryptic and relies on
the programmer knowing the names of all the variables
at write-time.
--
Steven D'Aprano
More information about the Python-list
mailing list