Filter and lambda expressions with dictionaries. Update: Answered
Hi You all, I have the following questions. I have a dictionary with disney figures and I would like to filter out all the names that are not 'Klaartje' # declaring dictionary 1 figures = {'chicken':'Klaartje', 'horse': 'Karel', 'duck' : 'Donald'} # def function that filters all options from which the value of the item is equal to 'Klaartje' by using # item[1] and figures.items() 2 k_names = dict(filter(lambda item:item[1] == 'Klaartje', figures.items())) 3 print(k_names) Output {'chicken': 'Klaartje'} Now I try to generate the same output but in a different way # def function that filters all options from which the value of the item is equal to 'Klaartje' by using # value and figures.values() 2 k_names = dict(filter(lambda value : value == 'Klaartje', figures.values())) 3 print(k_names) This time, I get an error. Same happens when I try to filter out by keys. # def function that filters all options from which the key of the item is equal to 'horse' by using # key and figures.keys() 2 k_names = dict(filter(lambda key : key == 'horse', figures.keys())) 3 print(k_names) Does anyone know if this is even possible, and if so, how? The reason I want to know, is because I would like to filter out not just the names that are not 'Klaartje', but al the names that do not start with a 'K'. Normally you would use name[0} != 'K', but now I use the [] for to determine the key/value of the item I am iterating over, so this does not work. I hope one of you can help me. Thankyou in advance!