Skip to main content

Section 9.15 List Methods

The dot operator can also be used to access built-in methods of list objects. append is a list method which adds the argument passed to it to the end of the list. Continuing with this example, we show several other list methods. Many of them have names that give you a good clue as to what they do.
There are two ways to use the pop method. The first, with no parameter, will remove and return the last item of the list. If you provide a parameter for the position, pop will remove and return the item at that position. Either way the list is changed.
The following table provides a summary of the list methods shown above. The column labeled result gives an explanation as to what the return value is as it relates to the new value of the list. The word mutator means that the list is changed by the method but nothing is returned (actually None is returned). A hybrid method is one that not only changes the list but also returns a value as its result. Finally, if the result is simply a return, then the list is unchanged by the method.
Another word for functions which take lists as arguments and change them during execution are called modifiers and the changes they make are called side effects.
Be sure to experiment with these methods to gain a better understanding of what they do.
Table 9.15.1.
Method Parameters Result Description
append item mutator Adds a new item to the end of a list
insert position, item mutator Inserts a new item at the position given
remove item mutator Removes the first occurrence of item
pop none hybrid Removes and returns the last item
pop position hybrid Removes and returns the item at position
sort none mutator Modifies a list to be sorted
reverse none mutator Modifies a list to be in reverse order
index item return idx Returns the position of first occurrence of item
count item return ct Returns the number of occurrences of item
Details for these and others can be found in the Python Documentation 1 .

Note 9.15.2. Using .index with lists.

As with the .index method for strings, if an item does not exist in a list, the program will generate a ValueError. Run this program and enter 2 for the number to find:
It is important to remember that methods like append, sort, and reverse all return None. This means that re-assigning mylist to the result of sorting mylist will result in losing the entire list. Calls like these will likely never appear as part of an assignment statement (see line 8 in the following program).

Checkpoint 9.15.3.

    What is printed by the following statements?
    a_list = [4, 2, 8, 6, 5]
    a_list.append(True)
    a_list.append(False)
    print(a_list)
    
  • [4, 2, 8, 6, 5, False, True]
  • True was added first, then False was added last.
  • [4, 2, 8, 6, 5, True, False]
  • Yes, each item is added to the end of the list.
  • [True, False, 4, 2, 8, 6, 5]
  • append adds at the end, not the beginning.

Checkpoint 9.15.4.

    What is printed by the following statements?
    a_list = [4, 2, 8, 6, 5]
    a_list.insert(2, True)
    a_list.insert(0, False)
    print(a_list)
    
  • [False, 4, 2, True, 8, 6, 5]
  • Yes, first True was added at index 2, then False was added at index 0.
  • [4, False, True, 2, 8, 6, 5]
  • insert will place items at the index position specified and move everything down to the right.
  • [False, 2, True, 6, 5]
  • insert does not remove anything or replace anything.

Checkpoint 9.15.5.

    What is printed by the following statements?
    a_list = [4, 2, 8, 6, 5]
    temp = a_list.pop(2)
    temp = a_list.pop()
    print(a_list)
    
  • [4, 8, 6]
  • pop(2) removes the item at index 2, not the 2 itself.
  • [2, 6, 5]
  • pop() removes the last item, not the first.
  • [4, 2, 6]
  • Yes, first the 8 was removed, then the last item, which was 5.

Checkpoint 9.15.6.

    What is printed by the following statements?
    a_list = [4, 2, 8, 6, 5]
    a_list = a_list.pop(0)
    print(a_list)
    
  • [2, 8, 6, 5]
  • a_list is now the value that was returned from pop(0).
  • [4, 2, 8, 6, 5]
  • pop(0) changes the list by removing the first item.
  • 4
  • Yes, first the 4 was removed from the list, then returned and assigned to a_list. The list is lost.
  • None
  • pop(0) returns the first item in the list, so a_list has now been changed.

Note 9.15.7.

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range