constructors for lists and tuples

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

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

  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> wrote in message
news:1f5252d4.0112110519.2617d639 at

> 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 [] 
via Mailgate.ORG Server - http://www.Mailgate.ORG

More information about the Python-list mailing list