I have a list say for example , a =["1","2"]
i want , c =1 and d = 2 . I can do c = a[0] , d = a[1] which will give me the required output , but if the list contents are dynamic ? How to store each elements in a list in a variable.
Thanks
I have a list say for example , a =["1","2"]
i want , c =1 and d = 2 . I can do c = a[0] , d = a[1] which will give me the required output , but if the list contents are dynamic ? How to store each elements in a list in a variable.
Thanks
One way (bad practice, but works):
x = ord('a')
list = ["1", "2"]
for i in list:
exec('%s="%s"' % (chr(x), i))
x+=1
But I would personally go this way:
from collections import namedtuple
list = ["1", "2"]
names = ["a", "b"]
d = dict(zip(names,list))
obj = namedtuple('X', d.keys())(*d.values())
Then, you can use obj.a
, obj.b
, etc.