5

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
Goran
  • 6,644
  • 11
  • 34
  • 54

1 Answers1

8

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' 
Hackaholic
  • 19,069
  • 5
  • 54
  • 72