Question: Write a function called get_avg(). Given a dictionary and a key, get_avg() returns the average of all the elements in the list located at the

Write a function called get_avg().

Given a dictionary and a key, get_avg() returns the average of all the elements in the list located at the given key.

Notes:

  • If the list at the given key is empty, it should return 0.
  • If the property at the given key is not a list, it should return 0.
  • If the key doesn't exist within the dictionary, it should return 0.

obj = {'key': [1, 2, 3]}

output = get_avg(obj, 'key')

print(output) # --> 2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!