Home>

I want to declare a variable according to the number of elements in array A (which changes dynamically) and put elements in the declared variable as shown below.

A = ['a','b','c','d']
num1 = a
num2 = b
num3 = c
num4 = d


Instead of manually defining this as num1 = A [0], num2 = A [1] ..., variables (num1, num2 ...) are automatically generated according to the number of elements in array A. , I want to store the elements of array A there.

It was difficult to find out, so I asked a question.
I would appreciate it if you could give me some advice.

  • Answer # 1

    A = ['a','b','c','d']
    for i in range (len (A)):
        exec ('num' + str (i + 1) +'= A [i]')
    print (num1)
    # a