I have 5 variables a,b,c,d and e and I am making a list of those variables. Now I want to loop over the name of them.
a=[1,3,4]
b=[4,5,6]
...
data=[a,b,c,d,e]
for x in ----:
print(x)
and the output should be like
a
b
c
d
e
thanks!
I have 5 variables a,b,c,d and e and I am making a list of those variables. Now I want to loop over the name of them.
a=[1,3,4]
b=[4,5,6]
...
data=[a,b,c,d,e]
for x in ----:
print(x)
and the output should be like
a
b
c
d
e
thanks!
You can't do so without changing the data structure of data
; the list does not store the "name" of the variables it carries.
(The last sentence might sound a little handwavy to some people; actually you are dealing with objects, which can be accessed by certain names a
, ..., e
. When you make [a, b, c, d, e]
, you are creating a list that has a direct access to those objects. The names are not relevant to this list; it just gets to the objects directly.)
One thing you can do is to make a dictionary:
data = {'a': a, 'b': b, 'c': c, 'd': d, 'e': e}
for k in data:
print(k)
In this way you store the name tag of each item, as well as the its value.