How to store variables in arraylist in python

I want to have something like

var1 = ('a','b','c')
var2 = ('a1','b1','c1')
var3 = ('a2','b2','c2')
var4 = ('a3','b4','c5')
var5 = ('a6','b6','c6')

How can I get all the ones in one var variable

I have a loop that will save each array, but I want to have only one variable

+3
source share
1 answer
var = [var1, var2, var3, ('a3', 'b4', 'c5'), var5]

You can also copy var i in a loop, but this is a very bad programming practice. Basically, you should create a list up and not later from the variable names. If you have to do this, here's how:

var = [locals()['var' + str(i)] for i in range(6)]

This is a longer form:

var = []
for i in range(6):
    var.append(locals()['var' + str(i)])
+6
source

All Articles