Skip to main content

Section 12.2 Writing-Lists-WE1-P1

Subgoals for Writing Lists.

  1. Instantiating a list variable
    1. Craft name of variable.
    2. 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.
    3. Assign the values to be stored in the list using square brackets or the appropriate function call.
  2. Accessing list element
    1. 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.
    2. listName[index] returns value stored at that index.
    3. Index must be between 0 and len(listName)-1, inclusive, or a negative value; otherwise an IndexError exception occurs at runtime.
  3. Slicing multiple values from a list
    1. Determine the range of indexes for the elements to be sliced
    2. listName[startIndex:endIndex] returns a new list containing the elements from startIndex to endIndex-1 (inclusive)
    3. Negative numbers can be used for startIndex and endIndex to count from the end of the list
    4. Omitting startIndex starts from the beginning of the list, and omitting endIndex goes to the end of the list
  4. Adding or changing value of a list element
    1. 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.
    2. 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.
      1. collection_name.append(new_value) - add a new element to the end of the list.
    3. 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.
      1. collection_name.insert(index, new_value) - add a new element at the specified index in the list.
    4. If changing a value already in the list, use an assignment statement to update the value at the appropriate index.
      1. Determine value of index of element to be changed (remember rules for index values)
      2. Determine the expression for RHS
      3. Write assignment statement to update list element
  5. Traversing a list
    1. Decide if updating in place or if only accessing.
    2. If accessing, write a normal for loop:
      1. 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.
    3. If updating in place, write a for loop using range and len:
      1. 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.
    4. Inside iteration, use loop control variable i as index into list, or var_name as value of list element
  6. Whole list actions
    1. Passing a list as an argument
      1. Determine that the entire list must be passed as an argument to a method by consulting documentation.
      2. 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.)
    2. List Assignment
      1. Determine that the reference to the list needs to be changed, not just its contents.
      2. The LHS of the assignment is the list reference needing to be changed
      3. 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.
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 1 of 3 activities on this page.