Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create variable names? [duplicate]

I have some logic which I need to repeat 6 times. Result is stored in variable at the end, but I need variable name to be something as data1, data2, data3, etc. So if I have:

for x in range(0, 3):
   ...some logic...
   data = result

How can I get data variable named as data1, data2 etc based on the loop number?

like image 572
Goran Avatar asked Jan 07 '15 22:01

Goran


1 Answers

you can use dictionary like this: examples

>>> a = ['hello', 'banana', 'apple']
>>> my_dict = {}
>>> for x in range(len(a)):
...     my_dict[x] = a[x]
... 
>>> my_dict
{0: 'hello', 1: 'banana', 2: 'apple'}

>>> my_dict[1]
'banana' 
like image 104
Hackaholic Avatar answered Oct 10 '22 11:10

Hackaholic