Lab: Lists

3 minute read

Preparation

These exercises should be completed with your assigned partner. Both of you will take turns playing the role of the driver (the one currently screen sharing) and the navigator. Please refer to the course Code of Conduct for more details.

Note: This lab we will be writing a lot of functions that manipulate lists instead of entire programs. I recommend creating a new file called lists_lab.py and putting all of your solutions to the following exercises in it.

Exercise 1

Write a function called list_double that takes a list of integers and returns a new list where each element of the list is double the size of the original list.

>>> t = [1, 2, 3, 4, 5]
>>> list_double(t)
[2, 4, 6, 8, 10]

Hint: You’ll need to create a new list with result = [] and add elements to the list one at a time using the append method.

Exercise 2

Write a function called list_double_in_place that takes a list of integers and modifies the list so that each element is double what it was before.

>>> t = [1, 2, 3, 4, 5]
>>> list_double_in_place(t)
>>> t
[2, 4, 6, 8, 10]

Hint: Since the function is not returning anything, you function should not include a return statement. Consider looping over the indices of the list so it is easier to modify the element at the current index.

Exercise 3

Write a function called sum that takes a list of integers and adds up the elements from the list. For example:

>>> t = [1, 2, 3, 4, 5, 6]
>>> sum(t)
21

Exercise 4

Write a function called nested_sum that takes a list of lists of integers and adds up the elements from all of the nested lists. For example:

>>> t = [[1, 2], [3], [4, 5, 6]]
>>> nested_sum(t)
21

Hint: You can use your solution from the previous exercise to compute the sum of each of the nested lists.

Exercise 5

Write a function called cumsum that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example:

>>> t = [1, 2, 3]
>>> cumsum(t)
[1, 3, 6]

Exercise 6

Write a function called middle that takes a list and returns a new list that contains all but the first and last elements. For example:

>>> t = [1, 2, 3, 4]
>>> middle(t)
[2, 3]

Note: Since the function should return a new list rather than modifying the old one, you should consider using string slicing here.

Exercise 7

Write a function called chop that takes a list, modifies it by removing the first and last elements, and returns None. For example:

>>> t = [1, 2, 3, 4]
>>> chop(t)
>>> t
[2, 3]

Note: You should also consider using the pop method to modify the given list instead of creating a new one.

Exercise 8

Write a function called is_sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and False otherwise. For example:

>>> is_sorted([1, 2, 2])
True
>>> is_sorted(['b', 'a'])
False

Hint: Since you’ll need to be comparing adjacent elements to make sure that they are sorted, consider looping over the indices of the list so it is easier to look up the next or previous element.

Exercise 9

Write a function called has_duplicates that takes a list and returns True if there is any element that appears more than once. It should not modify the original list.

Acknowledgment:

These exercises were originally written by Allen Downey in his open source book Think Python 2e. Downey's book is licensed under the GNU Free Documentation License, which allows users to copy, modify, and distribute the book.

These exercises were modified by Titus H. Klinge in 2021 and presented above under the same license in order to better serve the students of this course.