Skip to main content
Contents
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.4 Writing-Lists-WE2-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.4.1
Exercises Exercises
1.
Q45: What are the contents of list alpha after this code has been executed?
alpha = [10, 20, 30, 40, 50, 60, 70]
start = alpha[-1]
for i in range(1, len(alpha)):
alpha[i] = alpha[i-1]
alpha[0] = start
[20, 30, 40, 50, 60, 70, 10]
[10, 20, 30, 40, 50, 60, 70]
[10, 10, 20, 30, 40, 50, 60]
[70, 10, 10, 10, 10, 10, 10]
[10, 10, 10, 10, 10, 10, 10]
2.
Q46: What are the contents of list
beta
after this code has been executed?
beta = [10, 20, 30, 40, 50, 60, 70]
for i in range(1, len(beta)):
beta[i] = beta[i-1]
[20, 30, 40, 50, 60, 70, 10]
[10, 20, 30, 40, 50, 60, 70]
[10, 10, 20, 30, 40, 50, 60]
[70, 10, 20, 30, 40, 50, 60]
[10, 10, 10, 10, 10, 10, 10]
3.
Q47: What are the contents of list
gamma
after this code has been executed?
gamma = [10, 20, 30, 40, 50, 60, 70]
for i in range(1, len(gamma)):
gamma[i] = gamma[i+1]
[20, 30, 40, 50, 60, 70, 70]
[20, 30, 40, 50, 60, 70, 10]
[10, 20, 30, 40, 50, 60, 70]
[10, 10, 20, 30, 40, 50, 60]
You have attempted
of
activities on this page.