The function below takes two arguments, a dictionary called dog_dictionary and a list of dog names (strings) adopted_dog_names. The dog dictionary uses dogs' names as the keys for the dictionary. Complete the function to remove all the adopted dogs from the dog_dictionary. Return the dictionary at the end of the function.

Respuesta :

Answer:

The solution code is written in Python.

  1. def removeAdoptedDog(dog_dictionary, adopted_dog_names):
  2.    for x in adopted_dog_names:
  3.        del dog_dictionary[x]
  4.    
  5.    return dog_dictionary  
  6. dog_dictionary = {
  7.    "Charlie": "Male",
  8.    "Max" : "Male",
  9.    "Bella" : "Female",
  10.    "Ruby": "Female",
  11.    "Toby": "Male",
  12.    "Coco": "Female",
  13.    "Teddy": "Male"
  14. }
  15. print(dog_dictionary)
  16. adopted_dog_names = {"Ruby", "Teddy"}
  17. print(removeAdoptedDog(dog_dictionary, adopted_dog_names))

Explanation:

Firstly, let's create a function removeAdoptedDog() takes two arguments, dog_dictionary & adopted_dog_names (Line 1)

Within the function, we can use a for-loop to traverse through the adopted_dog_names list and use the individual dog name as the key to remove the adopted dog from dog_dictionary (Line 3). To remove a key from a dictionary, we can use the keyword del.

Next return the updated dog_dictionary (Line 5).

Let's test our function by simply putting some sample records on the dog_dictionary (Line 7 -15)  and two dog names to the adopted_dog_names list (Line 19).

Call the function removeAdoptedDog() by passing the dog_dictionary and adopted_dog_names as arguments. The display result will show the key represented by the adopted_dog_names have been removed from the dog_dictionary (Line 21).

ACCESS MORE