Home > Blockchain >  Comparing dictionary values with a list and returning the key
Comparing dictionary values with a list and returning the key

Time:10-12

I am almost there with my code, and I think I need to make a minor change to return the desired output but really I have encountered a 'coder's block'(if there is anything like that). Really appreciate any help: my code:

def no_you_pick(my_dict, my_list):
    output = []
    for i in my_list:
        for k, v in my_dict.items():
            if i in v:
                output.append(k)
                output.sort()
                return output
        else:
            return "Sorry, no restaurants meet your restrictions"

grading_scale={'Moes, Larrys, Curlys': ['vegetarian', 'vegan', 'gluten-free'], 'Hot Chili Pepper': ['vegetarian', 'gluten-free', 'vegan']}
guests_diet = ["vegetarian", "vegan", "gluten-free"]
print(no_you_pick(grading_scale, guests_diet))

Output:

['Moes, Larrys, Curlys']

Desired output:

["Hot Chili Pepper", "Moes, Larrys, Curlys"]

PS: I have also tried to return output in line with the nested for loop but it returns ["Hot Chili Pepper"] *I do not want to use list comprehension as I do not want the else statement printed within [] brackets.

CodePudding user response:

You are returning too early (from within the loop) and you can shorten this, of course:

def no_you_pick(my_dict, my_list):
    output = [k for k, v in my_dict.items() if any(x in v for x in my_list)]
    if output:
        output.sort()  # only sort it once
        return output
    return "Sorry, no restaurants meet your restrictions"

CodePudding user response:

You should add more example of what you expect with different inputs.
First you only get one result in output because you return as soon as you find a match.
Second, you should avoid adding a result multiple times if their dishes match multiple inputs: I think output should be a set.
Finally, why would you return a string when otherwise a list is expected? It's okay to return [] or {}, you can print your warning nonetheless:

def no_you_pick(my_dict, my_list):
  output = set()
  for i in my_list:
      for k, v in my_dict.items():
          if i in v:
              output.add(k)
  if not output:
      print("Sorry, no restaurants meet your restrictions")
  return output
grading_scale={'Moes, Larrys, Curlys': ['vegetarian', 'vegan', 'gluten-free'], 'Hot Chili Pepper': ['vegetarian', 'gluten-free', 'vegan']}
guests_diet = ["vegetarian", "vegan", "gluten-free"]
print(no_you_pick(grading_scale, guests_diet))

CodePudding user response:

The issue in your code is that you are returning the first value you find. Don't forget that calling return exits your function. A solution could be adding the return line outside the forloop.

A simpler way to do this would be:

def no_you_pick(my_dict, my_list):
    output = [x[0] for x in my_dict.items() if set(my_list).issubset(x[1])]
    if len(output) > 0:
        return output
    return "Sorry, no restaurants meet your restrictions"
  • Related