2021年1月4日星期一

Traversing a Dictionary

Problem: Write a function called find_value that takes two parameters. The first parameter, called search_dict, is a dictionary. The second parameter, val_2_find, is a specific value to find in the dictionary. If the value is in the dictionary, it should return a list of the corresponding key or keys (if the value occurs more than once). If the value is not in the dictionary it should return the phrase "That value does not exist!".

def find_value(search_dict, val_2_find):      lst = []             for k, y in search_dict.items():          if val_2_find in y:              lst.append(k)          else:                               "That value does not exist!"                                                            return lst       print(find_value({'key1': 'bird', 'key2': 'mineral', 'key3': 'animal', 'key4': 'animal', 'key5': 'animal'}, 'animal'))  

Question: How do I return the Else without breaking out of the for loop?

https://stackoverflow.com/questions/65572561/traversing-a-dictionary January 05, 2021 at 10:49AM

没有评论:

发表评论