Section 11.13 Worked Example: Lists - Minimum Value
Subgoals for Evaluating Lists.
-
Declaring and initializing a list
- Set up a one dimensional table (i.e., one row) with 0 to
size - 1
elements. - Upon instantiation of a list, all elements contain values from the initializer list (i.e., the list of values inside the square brackets). If no initializer list is provided, the list is empty.
-
Determining access, slicing, changing, adding, or whole list actions.
- Determine the list name and the action to be performed.
- If the list is on the left hand side of an assignment statement WITHOUT square brackets, it is a Whole List Action for List Assignment.
- If the list is on the left hand side of an assignment statement WITH square brackets, it is a Changing value of a List Element.
- If the
append
orinsert
method is being used on an instance of a list, it is an Adding a Value to a List Element. - If the list is an expression WITHOUT square brackets, it is a Whole List Action for Passing a List as an Argument.
- If the list is an expression WITH square brackets, then check if there is a colon in the square brackets. If there is a colon inside of the square brackets, it is a Slicing Multiple Values from a List. Otherwise, it is an Accessing List Element.
-
Accessing list element
- 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
andlen(listName)-1
, inclusive, or a negative value; otherwise anIndexError
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 fromstartIndex
toendIndex-1
(inclusive)- Negative numbers can be used for
startIndex
andendIndex
to count from the end of the list - Omitting
startIndex
starts from the beginning of the list, and omittingendIndex
goes to the end of the list
-
Changing value of a list element
-
Evaluate expression within
[]
brackets to determine the index of the element to be changed, and the list to change.Determine value of index for element to be changed; a positive value if counting from the beginning, or a negative value if counting from the end. - Determine the expression of RHS (right-hand side) of the assignment statement.
- The lists’ value is now changed to match the value calculated from the RHS of the assignment statement.
-
-
Adding a value to a list element
- Check whether the
append
orinsert
method is being used. Note that either way you do not use an assignment statement, it is just a method call. - If
append
is used, the new value is added 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. Existing values starting from that index are shifted to the right.
-
Traversing a List
- Determine the list that is being iterated over. If the expression also involves a
range(len(list_name))
, then the list is being traversed by index. Therange
function can either take one argument (the length of the list) or 3 arguments (the starting index, ending index, and step size). If therange
function takes one argument, it will start at 0 and go to the length of the list - 1. If therange
function takes 3 arguments, it will start at the first argument and go to the second argument - 1, incrementing by the third argument. Otherwise, ifrange
is not used, then the list is being traversed by value. - Determine the loop control variable that is being used to iterate over the list. The loop control variable will take on each value or index in the list, one at a time, depending on whether we are iterating by value or by index.
- The loop control variable is used to access the list element in the body of the loop. If iterating by index, the loop control variable is used as an index to access or update the list element. If iterating by value, the loop control variable is used directly to access the list element (no updates are possible).
- The list can also be added to with the
append
orinsert
methods. Theappend
method adds a new value to the end of the list, while theinsert
method adds a new value at the specified index. Existing values starting from that index are shifted to the right.
-
Whole list actions
-
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. The list itself is not copied, so the function does not have its own copy of the list. However, the one exception to this is if you assign the argument to reference a different list in memory; then you will no longer be modifying the original list.
-
List Assignment
- 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 11.13.1
Problem: Assume that the integer list
alpha
has been properly declared and initialized with non-zero values. What does this code accomplish?min = alpha[0]
for i in range(1, len(alpha)):
if alpha[i] < min:
min = alpha[i]
Subsection 11.13.2 SG1: Declaring and initialization of list
There is no explicit declaration or initialization of a list within the code. However, the instructions describe that the list
alpha
has been properly initialized with non-zero values.
alpha
is a list of integers and has values, but we don’t know what those values are- However, we can still diagram a representation of this list
- notice that the largest index is size - 1
Subsection 11.13.3 SG2: Determine access or action
Within the loop, we are accessing list elements.
Subsection 11.13.4 Evaluating code
min = alpha[0]
The first line of the code sample initializes
min
to copy the value from alpha[0]
, which in our sample is 12.for i in range(1, len(alpha)):
if alpha[i] < min:
min = alpha[i]
- This loop has index
i
go from 1 to size-1 (<len(alpha)
) by increments of 1. - Then the value at
alpha[i]
is compared tomin
. If the value atalpha[i]
is less thanmin
, thenalpha[i]
is copied intomin
. - All indexes into the list are valid.
Let us trace with a sample list.

The first line of the code sample initializes
min
to copy the value from alpha[0]
, which in our sample is 12, and then a for-loop is used to traverse the list. The chart below uses one line to represent the memory and calculations during each iteration of the loop, starting when i
has a value of zero.i | alpha[i] | min | alpha[i] < min ? | |
1 | 22 | 12 | False | |
2 | 8 | 12 | True | min changes to 8 |
3 | 15 | 8 | False | |
4 | 2 | 8 | True | min changes to 2 |
We can see that each time a smaller value is located in the list, that value is stored in
min
.What does this code accomplish?
Answer.
min
contains the smallest value found in the list alpha
.Subsection 11.13.5 Practice Pages
You have attempted 1 of 1 activities on this page.