

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
These lecture notes from cpsc 121 (fall 2012) cover the topics of random numbers in python and writing a function to find the sum of all elements in sublists. Examples, hints, and solutions for exercises.
Typology: Study notes
1 / 3
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