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
Get step-by-step solutions from verified subject matter experts
