How to Search a Key inside a tuple within a Dictionary?

Hello,

My intention for this code would be to Search up Multiple Keys in the Dictionary and return the same Value.

However, the manual method of adding each key individually like so is tedious…
key1 = “bingo”
key2 =“bingo”
key3 = “bingo”

I was searching online and found out a single tuple with multiple keys in the Dictionary would work, however not exactly the way I imagined.

(Check Image)

The problem is that Python won’t look inside my tuple within the Dictionary, but will ONLY Search the Key Name itself. What are my options to get Python to Check the multiple keys inside the tuple?

The raw code is further below.

CO = (“q”,“Q”,“Quit”,“quit”)

dict_2 = { “eyes” : “black”, CO : “quit” }

if “Q” in dict_2: #Intention is to Get Python Dictionary to Check within My “CO” Variable

print(True)                       # For the Key "Q"

else:

print(False)                      # Only Returns False...

you could do that with a function (that loops through the keys of the dict to check for a tuple) as below:

dict_1 = {"eyes": "black", ('q','Q'): "quit" } def check_dict_1_for(thing): if thing in dict_1: return True for key in dict_1: #if (thing == key): #return True if isinstance(key, tuple): if thing in key: return True return False print(check_dict_1_for('Q'))

but I think it would be easier just to have multiple keys with the same value:

dict_2 = {
     "eyes": "black",
     'q': "quit",
     'Q': "quit",
     "quit": "quit",
     "Quit": "quit" }

alternatively, you could do a dict comprehension

dict_2 = { "eyes": "black" }
dict_2.update({ thing: "quit" for thing in ('q', 'Q', "quit", "Quit") })

Hey,

Thanks for replying!

I think the tuple checking loop is a good alternative option if only, I can just find a way for Python to Match with the keys in the “Tuple itself”. (reference values within the tuple for a Matching Key! )

Not sure, which function or direction to obtain that result…

Of course, the obvious method of having to enter multiple keys with the same value is the current working model for my script, just wanted to get a 2nd opinion on another approach, perhaps using tuples to make the dictionary look less messy.

It just doesn’t look good to the User when you “list” out all the keys in the Dictionary, and there are multiple keys with somewhat similar characters matching with the exact same value.

Thanks again!

Just throwing this out there…

>>> CO = ('q', 'Q', 'Quit', 'quit')
>>> k = 'Quit'
>>> k in CO and 'quit' or None
'quit'
>>> k = 'wit'
>>> k in CO and 'quit' or None
>>> 
1 Like

you can also use a similar function to get the value for a specific key (or something in a tuple)
the same way:

dict_1 = {"eyes": "black", ('q','Q'): "quit" } def get_dict_1_value_for(thing): if thing in dict_1: return dict_1[thing] for key in dict_1: #if (thing == key): #return dict_1[thing] if isinstance(key, tuple): if thing in key: return dict_1[key] #return None print(get_dict_1_value_for('Q'))
1 Like

That was Brilliant! I just started out programming for a Month, and this was the biggest challenge for my first coding program. I was able to make a pragmatic script but couldn’t move forward, once this tuple issue came up. Only imagining some possibilities, but not a satisfying answer. This was like a door opener for me, since I learned soo much from your coding advice.

Being able to access the keys inside a tuple that’s held in a dictionary is awesome!

Again, thank you soo much for the help!

With that said, I only get half your code!

You mind verifying my understanding of your code starting from line 10 and below?

I don’t know what does the “ininstance” function do, until I just saw it. .
I’m using VCS, and the linter is claiming that’s its a function to check within the tuple. Yet, its also saying “return whether an object is an instance of a class or a subclass thereof” . . .

However,
I’m assuming this is the special function I was hoping for, which tells python to check if a key is inside the tuple,
if so, return that key from the dictionary?

The loop in question (from the earlier Codebyte):

  for key in dict_1:
    #if (thing == key):
      #return dict_1[thing]
    if isinstance(key, tuple):
      if thing in key:
        return dict_1[key]

The loop is going through all the keys of the dictionary, one at a time.
isinstance(key, tuple) checks if key is a tuple (it gives True if key is an instance of tuple, False otherwise).
Another similar built-in function is type.
type(key) would give you the data type of key (possibly in an awkward format)

So, as its looping trough the keys of the dictionary, I want it to do do something special only if that key happens to be a tuple.

If the thing that we’re trying to match with happens to be inside this tuple, I want to return the value for the dictionary that goes with this tuple.
Note that the tuple is still the key we’re looking it;
so I want it to return dict_1[key] if something inside the tuple (- which is the key now) matches.

I hope that helps. I know my explanation might be unclear.


(And yes, the point of the function is that for a key, it gives the dictionary's value for the key, or if you match something inside a key [that's a tuple] it also gives you the appropriate value from the dictionary.)
1 Like

Thank you! for the explanation!

Its a clear explanation, I just need to become more comfortable with using it in my program.