I&C SCI 32 Lecture Notes - Lecture 3: Function Type

45 views3 pages

Document Summary

You"ve no doubt seen, in previous coursework, how to iterate over the elements of a list in python using a for loop. """adds up the integers in a list of integers""" total = 0 for num in numlist: total += num return total. Now suppose we change the problem just slightly, so that we instead are given a list containing lists of integers (always two levels of depth), but maintain the same goal of summing all of the integers. Our function changes somewhat, because we need to handle the additional level of depth; we need to loop over the sublists in the list, then loop over the integers in the sublists. sum_numbers2. py def sum_numbers(numlist: [[int]]) -> int: """adds up the integers in a list of lists of integers""" total = 0 for sublist in numlist: for num in sublist: total += num return total.

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents

Related Questions