-1

Output = ['1)', 'JP', '*00000.0000/UNT', 0.07704, 61628.21, '0%(E)', 0.0, 'ND']

I have split my list item as above and would like to assign each value into separate variable something like below:

var1 = '1)' var2 = 'JP' .......

How can I accomplish it using for loop without need to manually specify how many variables are needed. In my example contains only 7 values, but in reality it could be less or more.

faizal_a
  • 93
  • 4
  • 15
  • 1
    No you probably don't really want to do that, even if you think you do. What are you trying to do? – alani Sep 13 '20 at 17:42
  • Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – alani Sep 13 '20 at 17:43
  • Technically they are already in "separate variables" namely `Output[0]`, `Output[1]`, `Output[2]` and so on, but these are not descriptive. You would need to know what each list element _means_ and then associate a name with each using a dictionary. (With this many elements, separate variables is not all that helpful, unless you had maybe 3 or less and this list itself was local to a small function.) The link from @alani is very appropriate here. – Ray Toal Sep 13 '20 at 17:46
  • @alani : i will need to insert the variable into database – faizal_a Sep 14 '20 at 01:15

3 Answers3

1

Don't assign each value to new variable. It will make things complicated. Just create a dictionary and work with key-value pairs, like below:

d={var1 : '1)', var2 : 'JP', .......}

and you can call them by d['var1'], d['var2'], etc whenever you want to use them

IoaTzimas
  • 10,538
  • 2
  • 13
  • 30
0

There are some possible solutions.

  1. You can access list elements directly: Output[0] or Output[3]
  2. It is possible to define variables programmatically in python, you can read more about it here: Programmatically creating variables in Python. But keep in mind that it is very bad practice so you probably don't want to use it.
  3. Looks like best way to solve this is to use dictionary comprehensions:
output = ['1)', 'JP', '*00000.0000/UNT', 0.07704, 61628.21, '0%(E)', 0.0, 'ND']
result = {f"var{index}": value for index, value in enumerate(output)}
print(result['var0']) # 1)
print(result['var1']) # JP

You can read more about dictionary comprehensions here: https://www.programiz.com/python-programming/dictionary-comprehension.

It will works with lists of any length. Also, notice, f"" string are working with Python 3.8+ so if you are using older version, replace it with "".format. More about string formatting here: https://realpython.com/python-string-formatting/

Fedoruka
  • 61
  • 5
0

I'm really don't understand why you want to do that and suggest you to reconsider, but you can try the following.

It's a bad practise!

class MyVariables():
    def create_variables(self, vars: list):
        for i, value in enumerate(vars, 1):
            setattr(self, f"var{i}", value)

c = MyVariables()
output = ['1)', 'JP', '*00000.0000/UNT', 0.07704, 61628.21, '0%(E)', 0.0, 'ND']
c.create_variables(output)
print(c.var1)

>> 1)
    
stilManiac
  • 454
  • 3
  • 13