-2

Like the title explains, let's say I have a dictionary like this:

dictionary = {apple: ["red", "healthy"], fries: ["yellow", "not healthy"], wall: ["is that eatable?"]} 

The length of each list can change but we know for sure that each value (all are strings) is stored in a list. Let's say i have the variable word = "is that eatable?" and i need to find which key have this value, how can i make this possible if they're stored in lists? Libraries are not allowed to solve this, thanks in advance for those who will try to help

Krist
  • 31
  • 6
  • 1
    *Libraries are not allowed.* Why? Is this a homework question? – C.Nivs Jan 29 '21 at 18:23
  • 2
    Pure code-writing requests are off-topic on Stack Overflow — we expect questions here to relate to *specific* programming problems — but we will happily help you write it yourself! Tell us [what you've tried](https://stackoverflow.com/help/how-to-ask), and where you are stuck. This will also help us answer your question better. – Timur Shtatland Jan 29 '21 at 18:34
  • Yes this is a homework question but is part of a large program that i've been working for 2 weeks already. @Timur Shtatland I'm not asking anyone to write code for me just give me an idea on I'm supposed to proceed in order to achieve this, large part of all the precedent code has been made by me but can't find online anything that explains this argument in depth they're all supposing we have strings in value instead of lists of strings – Krist Jan 29 '21 at 18:38
  • Invert the dict: make a new dict where the elements of the lists in the old dict are keys, and the old dict keys are values. Use that dict for lookups. – Timur Shtatland Jan 29 '21 at 18:45

1 Answers1

1

you can do

def find_key(s):
    for (key, values) in d.items():
        if s in values:
           return key

assuming s is in exactly one of the lists of the dictionaries.

Then apply the method to all words in your sentence.

user122644
  • 109
  • 1
  • I wouldn't suggest adding code. The OP's answer makes it seem like an assignment. I would suggest giving him enough info to do it on his own. – Ismail Hafeez Jan 29 '21 at 18:32
  • The example before was kind of dumb but in fact i need to check exactly the word and let's say the word is "I" (intended as me, myself) , that is basically true in way too many words so that's something that could give me false outputs. – Krist Jan 29 '21 at 18:42