initializing list with number of elements given by a variable

Alex Martelli aleax at aleax.it
Thu Apr 11 06:25:42 EDT 2002


Oliver Pust wrote:

> Hi,
> 
> I need to create a list which number of elements is given by an integer
> variable.
> 
> So something like this:
> 
>  >>> length_of_list = 6
>  >>> list = ????????? # what to place here, is the question

First, don't call it 'list': that's the built-in name of the list
type and it's best not to hide it with your own variable.  It is
much wiser to call it alist, somelist, whatever, NOT 'list'.

Apart from that, what you need is:

alist = length_of_list * [0]


Alex




More information about the Python-list mailing list