Jeewoo Lee
Jeewoo Lee

Reputation: 25

How do I search for number in tuple which is in dictionary values?

I am a coding newbie and I'm having a trouble writing this code:

my_data ={
    'a' : (900, (1, 2, 3, 11, 12))
    'b' : (200, (1,2,3))
                }

while True:
    month = int(input("which month?"))
    for key in my_data.keys():
        if month in my_data.keys()[1] :
            print ('')

Obviously this code won't work but I have no idea how to go further with it.

What I want the program to ultimately print is..:

>>>which month?3
>>>'a', 'b'

So... basically I don't know how to match/search the input data(month) in a tuple in a tuple in a value in a dictionary (whoa does this make sense??)

I'm very doubtful of my code, please tell me if this is not the recommended way.

Thank you

Upvotes: 2

Views: 60

Answers (4)

jez
jez

Reputation: 15359

Use .items() to get an iterator that lets you go through keys and values simultaneously. You can expand multiple variables (even at multiple levels) in a for statement (or, for that matter, any other kind of assignment). That makes the subsequent code more readable:

while True:
    month = int(input("which month?"))
    for key, (_, months) in my_data.items():
        if month in months:
            print(key)

The name _ is a convention for "variable I'm intending to ignore in the current context" but for readability you could always replace it with the natural name of whatever your 900 and 200 refer to.

Upvotes: 2

Mykola Mostovenko
Mykola Mostovenko

Reputation: 93

I'm not sure what your original dict is going to represent so no suggestions about it, but you can solve the mentioned issue with the code below:

my_data = {
    'a': (900, (1, 2, 3, 11, 12)),
    'b': (200, (1, 2, 3)),
    'c': (100500, (1, 2 ,5))
}
print('Enter `exit` to stop.')
while True:
    i = input("Which month?\n")
    if i.lower() == 'exit':
        break
    try:
        i = int(i)
    except ValueError:
        print('The int is expected as input')
    else:
        print(*(name for name, values in my_data.items() if i in values[1]), sep=', ')

This code will work as you asked

Enter `exit` to stop.
Which month?
3
a, b
Which month?
exit

To better understand this you need to read more about Python List Comprehensions i.e. here: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

What the code above really does it just iterates over your dict and prints only keys which values contains the number from input.

Upvotes: 1

AlexMTX
AlexMTX

Reputation: 72

my_data = {"a": (900, (1, 2, 3, 11, 12)),
           "b": (200, (1, 2, 3))}

while True:
    month = int(input("which month?"))
    for key, value in my_data.items():
        if month in value[1]:
            print(f"{key}: {value[0]}")

Upvotes: 1

Sushanta Das
Sushanta Das

Reputation: 131

Keep a condition to avoid infinite loop. The solution could be as below:

my_data ={
    'a' : (900, (1, 2, 3, 11, 12)),
    'b' : (200, (1,2,3))
    }
keys_where_exists = []
while True:
    month = int(input("which month?"))
    if month == -1:
        break
    for key, value in my_data.items():
        if month in value[1]:
            keys_where_exists.append(key)
print(keys_where_exists)

Upvotes: 0

Related Questions