[Tutor] declaring list in python

Brian van den Broek broek at cc.umanitoba.ca
Wed Jan 11 08:48:47 CET 2006

Logesh Pillay said unto the world upon 10/01/06 11:28 PM:
 > Hello list
 > I want to declare a list of a specific size as global to some nested
 > function like so

Hi Logesh,

what problem are you trying to solve by doing this? Knowing that will 
help generate more useful answers, I suspect.

 > def foo (n):
 > 	A[] (of size n)
 > 	def foo1
 > 		...
 > The only way I can think to declare the list is to use dummy values:
 > 	A = [0] * n
 > A = [] * n doesn't work.  [] * n = []

A = [None] * n

would be a better way to created an n-placed list of dummy values in 
Python, I think.

 > I'd prefer not to use dummy values I have no use for.  Is there any 

This is why knowing your problem would be helpful. Built-in Python 
data structures don't have size limitations that are declared when an 
instance of the data structure is created. (Python's not C.) There is 
no way (that I know of) to create an n-placed list save creating a 
list with n objects.

So, I think you will have to either give up on not employing dummy 
values or give up on creating a list of a fixed length.

You could subclass list to create a class with a max. and/or min. 
length, but I think knowing more about what you want to do would be 
helpful before getting into that :-)


Brian vdB

More information about the Tutor mailing list