-3

I am trying to use a Python list comprehension to get the Variable Name SW1 to equal the string "sw1" but I keep getting an error saying sw1 not defined

    VarList = ["SW1"]
    VarListEnd = ["sw1"]

    list3 = [exec("%s="%x + "%s"%y) for x in VarList for y in VarListEnd]
    list3

How do I amend the exec statement because that is where I think the error is? Really appreciate your help. Thanks in advance.

cordelia
  • 133
  • 2
  • 11

1 Answers1

0

You don't need exec for that. To create variables, you can do

for name, value in zip(VarList, VarListEnd):
    locals()[name] = value
blue_note
  • 25,410
  • 6
  • 56
  • 79