88.1k views
1 vote
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.

User JacekM
by
5.9k points

1 Answer

3 votes

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. return dog_dictionary
  5. dog_dictionary = {
  6. "Charlie": "Male",
  7. "Max" : "Male",
  8. "Bella" : "Female",
  9. "Ruby": "Female",
  10. "Toby": "Male",
  11. "Coco": "Female",
  12. "Teddy": "Male"
  13. }
  14. print(dog_dictionary)
  15. adopted_dog_names = {"Ruby", "Teddy"}
  16. print(removeAdoptedDog(dog_dictionary, adopted_dog_names))

Step-by-step 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).

User Manish Shrivastava
by
6.2k points