0

So Im quiet new to python and maybe I´ve searced the wrong words on google...

My current problem:

In python you can return the key to a value when its mentioned in a dictionary.

One thing I wonder, is it possible to return the key if the used value is part of a list of values to the key? So my testing skript is the following

MainDict={'FAQ':['FAQ','faq','Faq']}


def key_return(X):
    for Y, value in MainDict.items():
        if X == value:
            return Y
    return "Key doesnt exist"

print(key_return(['FAQ', 'faq', 'Faq'])) 

print(key_return('faq'))

So I can just return the Key if I ask for the whole list, How can I return the key if I just ask for one value of that list as written for the second print? On current code I get the "Key doesnt exist" as an answer.

3
  • if X == value or X in value: Commented Sep 10, 2021 at 15:19
  • Does this answer your question? Is there a short contains function for lists? Commented Sep 10, 2021 at 15:19
  • MainDict contains 1 key ('FAQ') which points to a value which is a list. The only way to get the value in this dict is to use MainDict['FAQ'] Commented Sep 10, 2021 at 15:19

3 Answers 3

1

You can check to see if a value in the dict is a list, and if it is check to see if the value you're searching for is in the list.

MainDict = {'FAQ':['FAQ','faq','Faq']}

def key_return(X):
    for key, value in MainDict.items():
        if X == value:
            return key
        if isinstance(value, list) and X in value:
            return key
    return "Key doesnt exist"


print(key_return(['FAQ', 'faq', 'Faq']))
print(key_return('faq'))

Note: You should also consider making MainDict a parameter that you pass to key_return instead of a global variable.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do this using next and a simple comprehension:

next(k for k, v in MainDict.items() if x == v or x in v)

So your code would look like:

MainDict = {'FAQ':['FAQ','faq','Faq']}

def key_return(x):
    return next(k for k, v in MainDict.items() if x == v or x in v)

print(key_return(['FAQ', 'faq', 'Faq'])) 
#FAQ
print(key_return('faq'))
#FAQ

Comments

0

You can create a dict that maps from values in the lists to keys in MainDict:

MainDict={'FAQ':['FAQ','faq','Faq']}
back_dict = {value: k for k,values in MainDict.items() for value in values}

Then rewrite key_return to use this dict:

def key_return(X):
    return back_dict[X]

print(key_return('faq'))

The line back_dict = {value: k for k,values in MainDict.items() for value in values} is a dictionary comprehension expression, which is equivalent to:

back_dict = {}
for k,values in MainDict.items():
    for value in values:
        back_dict[value] = k

This approach is more time-efficient that looping over every item of MainDict every time you search, since it only requires a single loopkup rather than a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.