-1

I'm trying to create a function that will do something like this:

list1 = ['a', 'b', 'c']
list2 = ['1', '2', '3']
option = input('your option: ')
printElements(option)

So if the user writes 'list1', the function should print a b c

2 Answers2

0

You can use ast.literal_eval to evaluate your variable and get the list you want.

printElements(ast.literal_eval(option))

A better way to do it would be to store your lists in a dictionary and then use option as a key:

d = {"list1": list1, "list2": list2}
printElements(d[option])
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
0

I think this is what you want:

list = input("Your option : ")
def printWords(list):
    for value in list:
        print(value)