constructors for lists and tuples

Rene Jensen chilopoda at hotmail.com
Tue Dec 11 11:05:44 EST 2001


Lists:
  MyList = [initializer] * size   e.g [None] * 5 = [None,None,...]

Tuples:
  MyTuple = (initializer, ) * size   e.g. (10,) * 5 = (10,10,10,10,10)

If you would want a list of initialized objects, do this:

  MyList = [None] * size
  MyList = map(lambda x:MyClass()  ,  MyList)

..should create <size> different objects rather than

  MyList = [MyClass()] * size

..that will give you a list of <size> references to the same object

"N Becker" <nbecker at fred.net> wrote in message
news:1f5252d4.0112110519.2617d639 at posting.google.com...

> what are the constructors for lists and tuples?  I wanted to make a
> list of a known size and then populate it.  Based on my experience
> with C++ and STL, I expected to find a constructor that took a size. 
> I would expect to find like:
> 
> list (size, init=None)
> 
> 2 problems.  
> 
> 1) I don't know how to do this
> 
> 2) I don't know where to find documentation on this.  All the modules
> document the constructors for their extensions, but looking at the
> library reference, I didn't see anything documenting the complete
> behaviour of the builtin list or tuple.




-- 
Posted from 0xc3f9f26c.mrgnxr1.ras.tele.dk [195.249.242.108] 
via Mailgate.ORG Server - http://www.Mailgate.ORG



More information about the Python-list mailing list