converting a variable value into a variable name and accessing previously stored values

May be you can make a dictionary to match the interest and the lists.

# some sample lists
shiraz_list = [["Wine name 1", "15", "sweet"], ["Wine name 2", "15", "sweet"]]
merlot_list = [["Wine name 3", "10", "sweet"], ["Wine name 4", "5", "dry"]]
cabernet_list = [["Wine name 5", "30", "sour"], ["Wine name 6", "20", "sweet"]]

# we can use dictionary to create key:value pairs, then get value by key 
# which is similar to get value by index when using list 
interest_match = {
    "shiraz": shiraz_list,
    "merlot": merlot_list,
    "cabernet": cabernet_list
}

interest = input()

# interest_match[interest] get the list by interest, e.g. interest_match["shiraz"] return shiraz_list 
for sublist in interest_match[interest]:
    print(sublist[0])

Output:

# Input: shiraz
Wine name 1
Wine name 2
# Input: merlot
Wine name 3
Wine name 4
# Input: cabernet
Wine name 5
Wine name 6

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top