-1

I want to assign for loop output values to variables in Python.

onetoten = range(1,5)
for count in onetoten:
    print (count)

The output of above for loop is as below

1
2
3
4

I want to assign this output to a list of variables like s1 = 1, s2 = 2, s3 =3 and s4 = 4. But there is no specific number that the list will be till 4. It may vary based on previous output in the script. So as per output of for loop the number of variable also must increased or based on number of output the variables should be used. Please help on this.

I asked same type of question earlier where i got a response to use enumerate but this is not exactly python and i am using the script in Jython so i don't have enumerate in Jython.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
  • 6
    No, you do *not* want to do this. There is no good reason to want to create an unknown number of variables. You should simply keep them in a list. – Daniel Roseman Jul 09 '13 at 15:08
  • You said you're using Jython, so I added that tag. Make sure you add relevant tags like that in the future. – thegrinner Jul 09 '13 at 15:18

4 Answers4

4

I want to assign this output to a list of variables like s1 = 1, s2 = 2, s3 =3 and s4 = 4. But there is no specific number that the list will be till 4. It may vary based on previous output in the script.

That implies that you do not want to use multiple variables. Store the values in a sequence s. Instead of sn you can then use s[n-1]:

>>> s = range(1,5)
>>> s[0]
1
>>> s[1]
2
etc.

If you require more flexible naming, i.e. specific identifier-value pairs, then you should consider using a dict as has been suggested.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • I agree with "_That implies that you do not want to use multiple variables._". Also, sequence (`list` or `tuple`) is not a good idea. Instead it should be `dict`, preferably. – Tadeck Jul 09 '13 at 15:11
  • 1
    Okay, after the update (`s[n-1]` instead of `s[n]`) it is good enough. – Tadeck Jul 09 '13 at 15:11
2

Use sequence unpacking if you know the number of variables:

>>> s1,s2,s3,s4 = range(1,5)
>>> s1
1
>>> s2
2
>>> s3
3
>>> s4
4

If you're talking about creating dynamic variables then it is a bad idea, I'd prefer a dict.

>>> dic = {}
>>> for x in range(1,5):
...     dic['s' + str(x)] = x
...     
>>> dic['s1']
1
>>> dic['s2']
2

You can create dynamic variables in python using globals() but it's not recommeded:

>>> globals()['foo'] = 'bar'
>>> foo
'bar'
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
1

If the number of variables you need to create is unknown to you before hand, or just large enough to make assigning them by hand unpractical, you can simply use a hash:

values = {}
for i, value in enumerate(range(1,5)):
    values['s%i' % (i+1)] = value

>>> values
{'s3': 3, 's2': 2, 's1': 1, 's4': 4}
>>> values['s1']
1
Robert Kajic
  • 8,689
  • 4
  • 44
  • 43
0

You can also use python list comprehension and have a list as result.

my_list = [x for x in range(1,5)]
print(my_list)

To get this result:

[1, 2, 3, 4]

And you can access to it with index

print("my_list[0] is: {}".format(my_list[0]))
Lorenzo Persichetti
  • 1,480
  • 15
  • 24