Skip to main content

Section 11.4 Writing-Lists-WE2-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 11.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]
  • IndexError Exception
You have attempted of activities on this page.