

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Today ...
Homework
Write a function called sublist sum(a list) that takes a list whose elements are lists of ints and returns the sum of all the values in each sublist. For example:
sublist_sum([[1,2], [2,3], [3,4]]) 15
sublist_sum([[1,2,3], [4,5,6]]) 21
Hint: Use a nested loop! Use either value loops or range loops
Answer:
def sublist_sum(a_list): total = 0 for sublist in a_list: for elem in sublist: total = total + elem return total