How can I check for lists within my dictionary?


I see that this works with single integer values but not when the value is a list. I thought that adding another for loop would allow me to into the lists but it throws an error. What am I missing here?


**In the below code I’ve edited the first print statement dictionary to include a value with a list “print(add_ten({1:[5, 10], 2:2, 3:3}))”

# Write your add_ten function here:
def add_ten(my_dictionary):
  for v in my_dictionary:
    my_dictionary[v] += 10
    for l in v:  
      my_dictionary[l] += 10 
  return my_dictionary
# Uncomment these function calls to test your  function:
print(add_ten({1:[5, 10], 2:2, 3:3}))
# should print {1:15, 2:12, 3:13}
print(add_ten({10:1, 100:2, 1000:3}))
# should print {10:11, 100:12, 1000:13}

Depending upon which dictionary key comes first, you’re bound to raise an error:

  • if the associated value is a list, then my_dictionary[v] += 10 is going to raise TypeError: can only concatenate list (not "int") to list
  • if the value is an int, then for - in will raise TypeError: 'int' object is not iterable

You’ll need to test with type() or isinstance() before deciding how to go about adding 10.