-4
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 :)

Finley
  • 1
  • 5
    I would make a dictionary, so you can just print the value of the required key. – Austin Sep 16 '20 at 16:24
  • 1
    Hi , welcome to SO, please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) as well as how to create a [minimal,complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) and edit your question with what you want to achieve and your try code – anky Sep 16 '20 at 16:25
  • @Austin agreed dictionary is definitely the way to go with this –  Sep 16 '20 at 16:27
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). “Show me how to solve this coding problem” is not a Stack Overflow issue. You have to make an honest attempt, and *then* ask a *specific* question about your algorithm or technique. – Prune Sep 16 '20 at 16:37

3 Answers3

2

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]
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • When you propose to use a dict, there is no need to use `eval` – Maurice Meyer Sep 16 '20 at 16:29
  • @MauriceMeyer I am not sure what the asker wants, so that's why I included both ways. Eval appears to be simple to some users, but is it a security risk – solid.py Sep 16 '20 at 16:32
  • 1
    I'd argue it is more complex to use `eval` as dictionaries can also group the data contained in the lists together if they are related, as well as providing other useful methods and exceptions. However, `eval` does do the job. – Leo Qi Sep 16 '20 at 16:35
  • 1
    @LeonisSupreme I agree for a larger code segment, `eval` would definitely hinder the developing or even testing process. I prefer the dict approach as well. I included `eval` as an example of what can be done, but maybe shouldn't be done. ;) – solid.py Sep 16 '20 at 16:37
0

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")

0

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]
Krishna Singhal
  • 631
  • 6
  • 9