0
x = [1,2,3]
y = [5,6,7]
z = input("Enter which variable to use x or y: ")

now I want use the variable x or y depending on whether the input is "x" or "y"

1

2 Answers 2

6

Please, please, please, please, please don't use runtime variable reading. Instead, just use a dict:

lists = {
    "x": [1, 2, 3],
    "y": [5, 6, 7]
}
z = input("Enter which variable to use x or y: ").strip()
print("Your variable is: " + str(lists[z]))
Sign up to request clarification or add additional context in comments.

Comments

1

You can use locals() if you really need it's very very unsafe.

x = [1,2,3]
y = [5,6,7]
z = input("Enter which variable to use x or y: ")
locals().get(z)

1 Comment

this is awesome , though this was not my question but happy to know locals()