Lists1 = [1,2,4,4]
Lists2 = [2,4,4]
Lists3 = [1,7]
List = input("input name of list to be printed: ")
How would I write the print command for this? Any help is appreciated :)
Lists1 = [1,2,4,4]
Lists2 = [2,4,4]
Lists3 = [1,7]
List = input("input name of list to be printed: ")
How would I write the print command for this? Any help is appreciated :)
Two ways, either by making a dictionary which includes all lists that are available for printing or by using eval
which is not recommended.
lists = {
'List1': [1,2,4,4],
'List2': [2,4,4],
'List3': [1,7]
}
text = input("input name of list to be printed: ")
print(lists[text])
Or
List1 = [1,2,4,4]
List2 = [2,4,4]
List3 = [1,7]
text = input("input name of list to be printed: ")
print(eval(text))
Output for both code segments:
input name of list to be printed: List1
[1, 2, 4, 4]
This code prints each element of the specified list.
Lists1 = [1,2,4,4]
Lists2 = [2,4,4]
Lists3 = [1,7]
List = input("input name of list to be printed: ")
if List == "Lists1":
for i in Lists1:
print(i)
elif List == "Lists2":
for i in Lists2:
print(i)
elif List == "Lists3":
for i in Lists1:
print(i)
else:
print("List doesn't exist")
You can achieve this by inserting them in dict
list_dict = {"Lists1":[1,2,4,4],
"Lists2":[2,4,4],
"Lists3" :[1,7]
}
List = input("input name of list to be printed: ")
print(list_dict.get(List))
#[1,2,4,4]