Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I pull out only the keys from the dictionary

I’m trying to create a dict to not use lots of if statements, but for some reason, I cannot seem to make it work the way I want. I’m trying to pull out only the keys from the dict when the correspond to the inputted day.

Thanks in advance.

edit: expected input/output

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Input (Day of Week) Output (Corresponding key)
‘Monday’ 12
‘Tuesday’ 12
‘Friday’ 12
‘Wednesday’ 14
‘Thursday’ 14
‘Saturday’ 16
‘Sunday’ 16
day = str(input())

day_price_dict = {12: ['Monday', 'Tuesday', 'Friday'], 14: ['Wednesday', 'Thursday'], 16: ['Saturday', 'Sunday']}

if day in day_price_dict:
    print(day_price_dict[day])

>Solution :

Following should do what you want:

# example: day = 'Tuesday'
day = str(input())

day_price_dict = {12: ['Monday', 'Tuesday', 'Friday'], 14: ['Wednesday', 'Thursday'], 16: ['Saturday', 'Sunday']}

# iterate through dict keys (12, 14, 16)
for key in day_price_dict:
    # if input is in the value list, print the key
    if day in day_price_dict[key]:
        # print 12
        print(key)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading