Unit 2: Programming Design

Lesson 9: List Methods

Notebook Authors

(fill in your two names here)

Facilitator: (fill in name)
Spokesperson: (fill in name)
Process Analyst: (fill in name)
Quality Control: (fill in name)

If there are only three people in your group, have one person serve as both spokesperson and process analyst for the rest of this activity.

At the end of this Lesson, you will be asked to record how long each Model required for your team. The Facilitator should keep track of time for your team.

Computational Focus: List Methods

Very often data is contained in as a list of items, for example numbers. Even data that is in a more complicated form like a spreadsheet or data frame is often converted to a list in order to calculate a statistic like the mean. For example if you want the mean of a column in a spreadsheet, it is often converted to a list before the mean is calculated. Lists can also contain other kinds of data, but for now we will be using lists of numbers.

Model 1: Appending to a List

Recall that a variable can hold multiple items in the form of a list. Each item of the list is separated by a comma and sandwiched between square brackets []. Each individual element of the list can be referenced by its index number, starting at 0 for the first position in the list.

A Python list has several built-in capabilities (methods). These methods are available to all lists.

Type the following Python code in separate Jupyter code cells:

list1 = [2, 4, 8]
len(list1)
print(list1)
print(list1[3])
list1.append(5)
print(list1)
print(list1[3])
list2.append(5)
list2 = []
print(list2[0])
list2.append(8)
print(list2)
print(list2[0])

Some lines will give you an error; you do not need to correct these errors.

Critical Thinking Questions

1. What is the result of applying the append method on a list?

2. What must be defined prior to the use of a list method like append?

3. Explain what caused the other errors in the example lines of code.

4. Describe the similarities and differences between using a Python list function like append compared to the Python built-in len function.

5. Write a Python assignment statement that sets the first element of a list1 to the last element of list2.

6. Describe the similarities and differences between using a Python list function versus using a function from the math library.

Model 2: More List Functions and Methods

A number of different built-in functions and list methods are defined in this Table and demonstrated below.

List Function / Method Description
len(L) identifies the number of elements of L
del(L[i]) delete the ith element from L
L.append(x) append a new element
L.count(x) count occurrences of x
L.index(x) find smallest position where x occurs in L
L.insert(i,x) insert x at position i in L
L.remove(x) remove first occurrence of x from L
L.reverse() reverse order of elements in L
L.sort() reorders the elements in L in increasing order

Type the following in separate Jupyter code cells:

list3 = [5,2,4,6,4]
len(list3)
list3.reverse()
print(list3)
list3.count(4)
list3.remove(4)
print(list3)
dir(list3) # feel free to also use `?dir` to help
help(list3)
help(list3.reverse)

Critical Thinking Questions

7. What is the result of applying the remove method on a list?

8. Give one example of a list method that requires an argument and one that does not.

9a. Is it necessary to use an assignment statement to change a list? Explain.

9b. How does this differ from the similar, list-like data type tuple?

10. What does the sort method do?

11. Write a Python assignment statement that uses the len function to access the last element of list2 and assign it to the variable last.

12. What is the median of list3 (the middle value in the list)? Describe which list methods and/or functions you would use and how in order to confirm this?

13. Write pseudocode that would prompt the user for numbers and build a new list of numbers by adding one number at a time into the list.  For now, do not worry about how the user will indicate they have entered all their values.

Model 3: User Input

A program can be designed in one of three ways to allow the user to signal when they are done entering values into the program:

  • The program could ask the user how many values will be entered, before starting the loop
  • The program could use a sentinel value, such as a negative number, to signal there are no more numbers
  • The program could repeatedly ask the user if more numbers will be entered (an ask-before-iterating loop)

Critical Thinking Questions

14. What type of program structure would be most appropriate to handle the first type of user input described in Model 3?

15. What is a potential problem with using a sentinel value for a program designed for scientists to build a list of numbers?

16. Consider how you would modify your list-building pseudocode (CTQ 13) so it uses an ask-before-iterating design. The program should only repeat if the user enters “yes” to the yes/no question.

Temporal Analysis Report

How much time did it require for your team to complete each Model?

Model 1:

Model 2:

Model 3: