Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store variables in arraylist in python

Tags:

python

linux

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 have all those in one variable var

I have a loop which will be saving each array but i want to have only one variable

like image 507
Mahakaal Avatar asked Oct 17 '25 00:10

Mahakaal


1 Answers

var = [var1, var2, var3, ('a3', 'b4', 'c5'), var5]

You could also copy vari in a loop, but that's very bad programming practice. Basically, you should construct a list upfront and not later from variable names. If you must do it, here's how:

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

This is the longer form of:

var = []
for i in range(6):
    var.append(locals()['var' + str(i)])
like image 85
phihag Avatar answered Oct 18 '25 15:10

phihag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!