0

lets assum that i have list bellow

a = ['hello', 'banana', 'apple','foo','bar','blablabla']

that i want to declare for each list item in new variable then print those variables later, how ever the lengh of my list.

In my Case the output of the program should be like this, call each variable and the values inside it:

variable_0 = hello
variable_1 = banana
variable_2 = apple
variable_3 = foo
variable_4 = bar
variable_5 = blablabla

And here the clear solution :

dic={}

a = ['hello', 'banana', 'apple','foo','bar','blablabla']
i = 0
string="variable_"
while(len(a) > i):
    dic.update({string+str(i):a[i]})
    print(string+str(i)+" = "+a[i])
    i+=1
  • 2
    Just to make sure there is no XY problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) can you explain why you want this? – ayhan Apr 27 '16 at 15:50
  • @ayhan: i already find some kind of question like mine but all of them solved using dictionary that is not my case here because i have to work on list now !! – Mohamed Bouabid Apr 27 '16 at 15:52
  • Sorry, I am not the one who closed your question. I just wanted to know your aim in doing this. – ayhan Apr 27 '16 at 15:55
  • 1
    I don't understand why you think the linked question does not answer your problem. The use case is exactly the same. – Daniel Roseman Apr 27 '16 at 16:00

0 Answers0