-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.

4
  • 1
    No you probably don't really want to do that, even if you think you do. What are you trying to do? Commented Sep 13, 2020 at 17:42
  • Does this answer your question? How do I create a variable number of variables? Commented Sep 13, 2020 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. Commented Sep 13, 2020 at 17:46
  • @alani : i will need to insert the variable into database Commented Sep 14, 2020 at 1:15

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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/

Comments

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)
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.