Q:

Assign different operators during python loop

I would like to assign loop results to a contineous operator, for instance:
for x in xrange(1, 5):
    answer = 5 + x

How can I replace this with:
for x in xrange(1, 5):
    answerx = 5 + x

Meaning, assigning the answers to answer1, answer2, etc.
Is this possible and how?

A:

Append answers to a list, or better still, use a list comprehension:
answers = [5 + x for x in xrange(1, 5)]

Now you can address answers[0], answers[1], etc.
You can also make it a dictionary:
answers = {'answer{}'.format(x): 5 + x for x in xrange(1, 5)}

Whenever you feel the urge to generate variable names from data, you are probably looking to use a dictionary or list instead. Keep data out of your variable names.

A:

Your best bet is to use a list:
answers = []
for x in xrange(1, 5):
    answers.append(5 + x)

Then you can access the answers as answers[0] and so on.
...but a list comprehension is even nicer, as in Martijn Pieters' answer.

