Skip to main content
Contents
Dark Mode Prev Up Next Scratch ActiveCode Profile
\(\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 12.2 Writing-Lists-WE1-P1
Subgoals for Writing Lists.
Instantiating a list variable
Determine if the list is empty or contains initial values. The initial values can be specific values, but they can also be the result of calling a function like
range
to generate a sequence of numbers and then converting that to a list.
Assign the values to be stored in the list using square brackets or the appropriate function call.
Determine value of index for element to be accessed; a positive value if counting from the beginning, or a negative value if counting from the end.
listName[index]
returns value stored at that index.
Index must be between
0
and
len(listName)-1
, inclusive, or a negative value; otherwise an IndexError exception occurs at runtime.
Slicing multiple values from a list
Determine the range of indexes for the elements to be sliced
listName[startIndex:endIndex]
returns a new list containing the elements from
startIndex
to
endIndex-1
(inclusive)
Negative numbers can be used for
startIndex
and
endIndex
to count from the end of the list
Omitting
startIndex
starts from the beginning of the list, and omitting
endIndex
goes to the end of the list
Adding or changing value of a list element
Decide if adding a new value to the end, adding a value elsewhere in the list, or changing an existing value inside of the list.
If adding to the end, use the
append
method to add the new value to the end of the list. Note that you do not use an assignment statement, it is just a method call.
collection_name.append(new_value)
- add a new element to the end of the list.
If adding a value elsewhere in the list, use the
insert
method to add the new value at the specified index. Note that you do not use an assignment statement, it is just a method call.
collection_name.insert(index, new_value)
- add a new element at the specified index in the list.
If changing a value already in the list, use an assignment statement to update the value at the appropriate index.
Determine value of index of element to be changed (remember rules for index values)
Determine the expression for RHS
Write assignment statement to update list element
Decide if updating in place or if only accessing.
If accessing, write a normal
for
loop:
for var_name in collection_name
- traverses collection_name from first element to last element storing a copy of each element from collection_name in var_name for each iteration of the loop.
If updating in place, write a
for
loop using
range
and
len
:
for i in range(len(collection_name))
- traverses
collection_name
from first element to last element storing the index of each element in
i
for each iteration of the loop.
Inside iteration, use loop control variable
i
as index into list, or
var_name
as value of list element
Passing a list as an argument
Determine that the entire list must be passed as an argument to a method by consulting documentation.
When calling a function, put variable name that represents the list as an argument in the method call. (Remember that when passing a list as an argument that changes made by the function to the list are persistent.)
Determine that the reference to the list needs to be changed, not just its contents.
The LHS of the assignment is the list reference needing to be changed
The RHS of the assignment is the new list reference
Subsection 12.2.1
Exercises Exercises
1.
Q1: Put the code in the appropriate order so that a
list
of integers will store multiples of 5 (from 100 down to 0) in reverse order.
def store_reverse():
---
reverse_fives = []
---
for i in range(20, -1, -1):
---
reverse_fives.append(i * 5)
---
return reverse_fives
2.
Q2. Which of the following would it be easier to use a list literal for versus a loop when creating a
list
? (Select all that are appropriate)
Initialize a list of author names called
favorite_authors
Initialize a list of numbers from 100 to 1000 called
nine_hundred
Initialize a list of ribbon colors called
ribbon_colors
Initialize a list with the top ten fastest times for your schoolβs track team in the 100 meter race named
top_ten
Initialize a list with the height (in inches) of all the children in a classroom called
classroom_heights
3.
Q3. Suppose you are writing a function that adds one to all of the elements of a
list
named
numbers
. Which of the following code snippets would be best for this task?
def add_one(numbers):
for i in range(len(numbers)):
numbers[i] = numbers[i] + 1
def add_one(numbers):
for num in numbers:
num += 1
This does not modify the original list because it only changes a copy of each value.
def add_one(numbers):
for num in numbers:
numbers[num] = num + 1
This misuses num
as both index and value.
def add_one(numbers):
for i in range(len(numbers)):
numbers[i] = i + 1
This sets each element to its index plus one, not to the original value plus one.
You have attempted
of
activities on this page.