Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ask user and print a list

I have a problem with printing a list. For example I have two lists:

a = [1,2,3,4,5]
b = [6,7,8,9,10]

Now I want to ask user to input a list name and then print that list.

name = input("Write a list name")

User entered "a"

for x in name:
    print(x)

But it does not work (not printing list "a"). Could You help me?

MORE INFO:

I have a dictionary:

poland = {"poznan": 86470,
          "warszawa": 86484,
          "sopot": 95266}

And lists:

poznan = [1711505, 163780, 932461, 1164703]

warszawa = [1503333, 93311, 93181, 93268, 106958, 106956, 127649, 106801, 107386, 93245, 154078, 107032]

sopot = [228481, 164126, 922891]

And now if user write "poznan" i want to assign ID of poznan from dictionary to variable "city_id" and then print a list with name "poznan"

like image 833
Maciej Avatar asked Dec 02 '22 15:12

Maciej


1 Answers

You need to map the lists to strings, which are what the user can enter.

So use a dictionary:

lists_dict = {
    'a': [1,2,3,4,5]
    'b': [6,7,8,9,10]
}

key = input("Write a list name")

print lists_dict[key]

Edit:

Your dictionary should look as follows:

poland = {
    "poznan": {"name": 86470, "lst": [1711505, 163780, 932461, 1164703]},
    "warszawa": {"name": 86484, "lst": [1503333, 93311, 93181, 93268, 106958, 106956, 127649, 106801, 107386, 93245, 154078, 107032]},
    "sopot": {"name": 95266, "lst": [228481, 164126, 922891]}
}

Access to your list should be done like so:

key = input("Write a list name")
# print the list under 'lst' for the dictionary under 'key'
# print poland[key]["lst"]
# EDIT: python 3's print a function, thanks @Ffisegydd:
print(poland[key]["lst"])
like image 166
Reut Sharabani Avatar answered Dec 10 '22 11:12

Reut Sharabani