1

Python noob here so forgive me if I have an easy question. I want to set the variable name to whatever value I am passing using my for loop.

roles = ["A" , "B" , "C"]
for role in roles:
    print (role)
    "outPutFor"+role = function_that_gives_outputforRole(role)

So I want my variables to read outPutForA , outPutForB and outPutForC .

but I am getting the below ERROR

    "outPutFor"+role = function_that_gives_outputforRole(role)
           ^
SyntaxError: can't assign to operator

What am I missing here ?

  • The error indicates the issue. See http://stackoverflow.com/questions/8956825/syntax-error-cant-assign-to-operator. – sgrg Apr 19 '17 at 14:50
  • 2
    You probably don't want to do that, presumably it's just better to have a dictionary `outPutFor` and use `outPutFor["A"]` as you would `outPutForA`. That way you can also iterate over all possibilities easily etc. – 098799 Apr 19 '17 at 14:51
  • 1
    You can not 'compute' the *name* of a variable. You can only compute the 'content' of a variable. That's why you should generalize the logic in your loop. `outPutForA` is the 'output' in the first iteration (when `role` is set to `'A'`). Hence, you might want to set `output = 'outPutFor' + role` or `output = 'outPutFor' + function_that_gives_outputforRole(role)`. – Michael Hoff Apr 19 '17 at 14:54
  • 1
    If you're really looking to dynamically create variables in your namespace, you can use `exec`, but I think the dictionary structures proposed below are much safer. – blacksite Apr 19 '17 at 14:55
  • @098799 Thanks. So if I want to get the value of this variable outside of the for loop, I could be able to do `print(outPutFor["A"])` ? –  Apr 19 '17 at 15:04
  • @JasonStanley Yes, this would print the value. You can do anything to this `outPutFor["A"]` object that you would to the variable `outPutForA` that you wanted. – 098799 Apr 19 '17 at 15:07

2 Answers2

0

You couldn't create a dynamically named variables. But you could use a python dict like

roles = ["A" , "B" , "C"]
outputs = {}
for role in roles:
    print (role)
    outputs["outPutFor"+role] = function_that_gives_outputforRole(role)

or create a dict with elements from 'roles' list as keys. Like

roles = ["A" , "B" , "C"]
outPutFor = {}
for role in roles:
    print (role)
    outPutFor[role] = function_that_gives_outputforRole(role)
    print(outPutFor[role])
kvorobiev
  • 5,012
  • 4
  • 29
  • 35
0

If you're looking for a way to store the output of each iteration of this function, you can create a list called results, for example, and for each role in roles, you can insert/assign the output of function_that_gives_outputforRole into the results list.

Example:

roles = ["A" , "B" , "C"]
results = []
for i, role in enumerate(roles):
    results[i] = function_that_gives_outputforRole(role)

In this example, you can access the output value for each role like this:

results[0] = function_that_gives_outputforRole("A")
results[1] = function_that_gives_outputforRole("B")
results[2] = function_that_gives_outputforRole("C")

You can also use a dict structure to store each output, as stated in kvorobiev's answer.

If you want, you can zip() these two lists into a single list to consolidate the input/output values.

Community
  • 1
  • 1
dot.Py
  • 5,007
  • 5
  • 31
  • 52