In: Computer Science
Write code that would go where the comment "#your line will go here" so that the code would successfully extract all names of dishes from the dictionary popular_foods that have at least one of the following ingredients:
popular_foods = {"Clam Chowder":
{"Country": "United States",
"Main Ingredients": ["clam", "onion",
"celery", "potato", "carrot",
"butter", "flour", "cream",
"red wine vinegar"]},
"Xiaolongbao":
{"Country": "China",
"Main Ingredients": ["flour", "pork",
"crab meat", "roe", "soy sauce",
"ginger", "rice wine",
"green onions"]},
"Feijoada":
{"Country": "Brazil",
"Main Ingredients": ["black beans",
"pork", "cabbage", "red peppers",
"onion", "tomato", "garlic"]},
"Ash Reshteh":
{"Country": "Iran",
"Main Ingredients": ["kidney beans",
"chickpeas", "navy beans",
"parsley", "spinach", "coriander",
"dill", "persian reshteh noodles",
"onions", "flour", "mint", "kashk",
"turmeric"]},
"Draniki":
{"Country": "Russia",
"Main Ingredients": ["potato", "onion",
"egg", "flour", "garlic"]},
"Ata Dindin":
{"Country": "Nigeria",
"Main Ingredients": ["palm oil",
"red bell pepper",
"scotch bonnet", "tomato",
"onion", "chicken stock",
"crayfish", "prawns", "mackerel"]}
}
dishes = []
for dish in popular_foods:
food_search = ["potato", "pork", "garlic"]
for food in food_search:
# your line will go here
dishes.append(dish)
break
The statement that fills the given code is
if food in popular_foods[dish]['Main Ingredients']:
Justification for the above statement:
food is an element in food_search list popular_foods[dish]['Main Ingredients'] means, is checks for the dish name that has the Main Ingredient from the food list.
The given popular_foods dictionary is nested in the following way:
popular_food = {"dish name":{"Country":"country name", "Main Ingredients":["ingredient1", "ingredient2", ...]}}
to check if the ingredient is present in a particular dish or not we use the followin code:
dishes = [] # an empty list that is later used to store the dish names that contain the given ingredients
# the following loop is used to iterate over every dish name in the popular_foods dictionary
for dish in popular_foods:
#the following is a list of items that are to searched in the
given dictionary
food_search = ["potato", "pork", "garlic"]
#the following loop iterates over the above list of items
for food in food_search:
#checks if the item in food_search is present in the Main
Ingredients of each dish in the popular_foods or not
if food in
popular_foods[dish]['Main Ingredients']:
# if any of the item from food_searc is in the Main Ingredients
of popular_food, then that dish name is appended to the list dish
and the loop breaks
dishes.append(dish)
break
Code:
popular_foods = {"Clam Chowder":
{"Country": "United States",
"Main Ingredients": ["clam", "onion",
"celery", "potato", "carrot",
"butter", "flour", "cream",
"red wine vinegar"]},
"Xiaolongbao":
{"Country": "China",
"Main Ingredients": ["flour", "pork",
"crab meat", "roe", "soy sauce",
"ginger", "rice wine",
"green onions"]},
"Feijoada":
{"Country": "Brazil",
"Main Ingredients": ["black beans",
"pork", "cabbage", "red peppers",
"onion", "tomato", "garlic"]},
"Ash Reshteh":
{"Country": "Iran",
"Main Ingredients": ["kidney beans",
"chickpeas", "navy beans",
"parsley", "spinach", "coriander",
"dill", "persian reshteh noodles",
"onions", "flour", "mint", "kashk",
"turmeric"]},
"Draniki":
{"Country": "Russia",
"Main Ingredients": ["potato", "onion",
"egg", "flour", "garlic"]},
"Ata Dindin":
{"Country": "Nigeria",
"Main Ingredients": ["palm oil",
"red bell pepper",
"scotch bonnet", "tomato",
"onion", "chicken stock",
"crayfish", "prawns", "mackerel"]}
}
dishes = []
for dish in popular_foods:
food_search = ["potato", "pork", "garlic"]
for food in food_search:
if food in
popular_foods[dish]['Main Ingredients']:
dishes.append(dish)
break
Code in the image: