Python: How to create new variables in for loops?

python

Solution

Variables name are tokens used as-is, i.e. variables aren't expanded inside other variable names.

You can't expect `a_i` to be equal to `a_1` if `i == 1`.

For that, use arrays or dictionaries.

a = {}
for i in range (0,3):
    a[i] = [i]
    a[i].append(i+1)
    print a[i]
print a
print a[1]

Problem

I'm trying to create several arrays in a loop and have access to them further on. I don't understand why I can modify and print them within the loop but outside it says the variable doesn't exist. ``` for i in range (0,3): a_i=[i] a_i.append(i+1) print a_i print a_1 ``` Is there anyone who can give me a suggestion on how to fix the problem?

Original source