RES: initializing list with number of elements given by a variabl e

Alves, Carlos Alberto - Coelce calves at coelce.com.br
Thu Apr 11 07:43:15 EDT 2002


It depends on how you should/want set the values of elements in the list.
For example, if you already know what/how elements to place in the list,you
could do:

lis=[None]*6 #Initialize the list with 6 None elements.
for i in range(6):
	lis[i]=i*i #or do something else

Now, lis is one list with 6 elements given by i*i.
If you need to enter values in run time, you could make it like this:

lis=[None]*6
for i in range(6):
	lis[i]=raw_input('Elemento %d:'%i)


> -----Mensagem original-----
> De: Oliver Pust [mailto:oliver.pust at web.de]
> Enviada em: quinta-feira, 11 de abril de 2002 06:23
> Para: python-list at python.org
> Assunto: initializing list with number of elements given by a variable
> 
> 
> 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
>  >>> print list
>  >>> [0,0,0,0,0,0]    # the resulting list should look like this
> 
> Can anybody help me here?
> 
> Thanks,
> 	Oliver
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20020411/a9fc753d/attachment.html>


More information about the Python-list mailing list