-4

I would like to know how to create lots of variables by looping it. I know other people have asked this before but everyone who knows says you need a good reason for it and to just set it in a dictionary. My reason is that I need to assign up to 6156119580207157310796674288400203776 variables and there is no way I can do that by typing them out. I need something like:

while counter < 1000:
    try[counter] = counter

So that I could do this:

>>> try837
837
>>>try453
453

etc.

(this is an example not the exact code but any answer for this will solve my problem)

I would also like to know why people are opposed to answering this particular question. I don't want to tax my computer more than I already am by assigning this many variables so if it is an issue that could harm my computer or my code I would like to know.

Andy
  • 49,085
  • 60
  • 166
  • 233

1 Answers1

3

You don't want to do this. Create a dictionary with a key for each suffix that you would use. Then use try[557] in place of the variable try557.

>>> try_ = dict((counter, counter) for counter in range(1000))
>>> print try_[557]
557

I'm using the standard technique of affixing an underscore to the otherwise reserved word "try".

(I'm ignoring the ludicrously large number of variables you claim to need.)

chepner
  • 497,756
  • 71
  • 530
  • 681