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?
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With