[Tutor] create 1000 000 variables
Kent Johnson
kent37 at tds.net
Sat Jul 15 03:49:04 CEST 2006
Сергій wrote:
> suppose I need to create 1000 000 variables
> var_1, var_2, .... var_1000000
> how to do this using for?
> (something like
> for i in range(1000000):
> ___var_
Rather than creating 1000000 variables, create a list with 1000000 values.
data = []
for i in range(1000000):
data.append(i*i) # or whatever data you want in your variables
This can be very nicely abbreviated with a list comprehension:
data = [ i*i for i in range(1000000) ]
In general, when you think you need to create a bunch of variables with
repetive names, you should probably use a list or dictionary instead.
Kent
More information about the Tutor
mailing list