More about variables

Alex alex at somewhere.round.here
Thu Apr 6 10:55:05 EDT 2000


> Let's say I had something like:
> 
> number_of_variables=raw_input('Enter number of variables: ')
> 
> if number_of_variables==1:
>    variable1=[]
> elif number_of_variables==2:
>    variable1=[]
>    variable2=[]

There is probably a more appropriate way of solving whatever problem is
behind this question.  How do you plan on accessing the variables once
they are created?

On the face of it, I think you might have more luck trying this sort of
arrangement:

number_of_variables=raw_input('Enter number of variables: ')
variables = []
for i in number_of_variables * [None]:
    variables.append ([])

However, if you are bent on using a separate name for each list, you
could do something like this:

for i in range (1, number_of_variables + 1):
    exec 'variable%i = []' % i
                
Alex.



More information about the Python-list mailing list