Question

I am wondering what I do in Python if I have a dictionary and I want to print out just the value for a specific key.

It will be in a variable as well as in:

dict = {'Lemonade':["1", "45", "87"], 'Coke:["23", "9", "23"] 'Water':["98", "2", "127"}
inp = input("Select key to print value for!" + "/r>>> ")
if inp in dict:
    #Here is where I would like it to print the Value list for the key that is entered.

I am running Python 3.3

No correct solution

OTHER TIPS

I have taken the liberty of renaming your dict variable, to avoid shadowing the built-in name.

dict_ = {
    'Lemonade': ["1", "45", "87"], 
    'Coke': ["23", "9", "23"], 
    'Water': ["98", "2", "127"],
}
inp = input("Select key to print value for!" + "/r>>> ")
if inp in dict_:
    print(dict_[inp])

As Ashwini pointed out, your dictionary should be {'Lemonade':["1", "45", "87"], 'Coke':["23", "9", "23"], 'Water':["98", "2", "127"]}

To print the value:

if inp in dict:
    print(dict[inp])

As a side note, don't use dict as a variable as it will override the built in type and could cause problems later on.

In Python 3:

# A simple dictionary
x = {'X':"yes", 'Y':"no", 'Z':"ok"}

# To print a specific value
print([value for value in x.values()][1])

Output:

no
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top