Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

question 5 consider the food dictionary below. write python code to ext…

Question

question 5
consider the food dictionary below. write python code to extract the list of fruits.
food = {
fruits: oranges, banana, pears, oranges,
grains : wheat, barley, corn
}
question 6
consider the food object again. extract the fruits and save it in an object my_fruits.
question 7
use the my_fruits object and return only the unique items in the list of fruits.

Explanation:

Question 5

Step1: Access the 'fruits' key

In Python, to access the value associated with a key in a dictionary, we use the syntax dictionary_name[key]. Here, the dictionary is food and the key is 'fruits'.

food['fruits']

Question 6

Step1: Access and assign

We access the 'fruits' key in the food dictionary and assign the result (the list of fruits) to the variable my_fruits.

my_fruits = food['fruits']

Question 7

Step1: Convert to set and back to list

A set in Python only contains unique elements. So, we first convert the my_fruits list to a set (which removes duplicates) and then convert it back to a list.

list(set(my_fruits))

Answer:

Question 5: food['fruits']
Question 6: my_fruits = food['fruits']
Question 7: list(set(my_fruits))