0
l1 = ["a","b","c"]
l2 = ["d","e","f"]

var1 = input("What list do you want to use? (l1 or l2)" 

print (var1)
output: l1 

how do I make it so if the user types in l1 the output will be: ["a","b","c"] ?

The closest example I have found is:

x='buffalo'    
exec("%s = %d" % (x,2))
Klaus D.
  • 13,874
  • 5
  • 41
  • 48

3 Answers3

3

A dictionary would be a great solution here.

mapping = {
    "l1": ["a","b","c"],
    "l2": ["d","e","f"]
}

var1 = input("What list do you want to use? (l1 or l2)")

mapping[var1]  # this is the list.

If var1 is not a key in the dictionary, though, the above code will raise a KeyError. You can check for this and print a useful error message with a try-except block.

try:
    my_list = mapping[var1]
except KeyError:
    print("unknown input: {}".format(var1))
jkr
  • 17,119
  • 2
  • 42
  • 68
2
if var1 == 'I1':
  print(I1)
elif var1 == 'I2':
  print(I2)

That's just a basic if/else.

Johnny
  • 211
  • 3
  • 15
-1

You might want to look at eval

Sample code:

l1 = ["a","b","c"]
l2 = ["d","e","f"]

var1 = input("What list do you want to use? (l1 or l2)")

print (var1)

eval("print(" + var1 + ")")

Output:

What list do you want to use? (l1 or l2)l1
l1
['a', 'b', 'c']
  • Do not use `eval` on user input. It is a major security risk. A user could theoretically delete all of your files. – jkr May 29 '20 at 01:43
  • eval was the missing piece! Thank you. Sorry, still a noob here so they will not let me upvote your answer. It is indeed the correct one. This is a soundboard, so I am not concerned about security, but thank you jakub – Vincent Clark May 29 '20 at 03:29