Skip to main content

Section 4.10 List Methods

The dot operator can also be used to access built-in methods of list objects. As you remember, unlike strings, lists are mutable. As a consequence, list methods can have different behaviors than string methods. Let’s look at these.
  • Return Type Methods: These methods work the same way as string methods: they give us a value but keep the original list the same.
  • Mutating Methods: These methods modify the list but do not return anything. Because of this, we shouldn’t use these methods on the right hand side of assignment (we shouldn’t assign them to a variable).
  • Hybrid Methods: These methods behave as both mutating and return type methods. They change the list and also return a value.
The following table provides a summary of the list methods shown above. The column labeled behavior gives an explanation as to what the return value is as it relates to the new value of the list.
Table 4.10.1.
Method Parameters Behavior Description
append item mutating Adds a new item to the end of a list
insert position, item mutating Inserts a new item at the position given
pop none hybrid Removes and returns the last item
pop position hybrid Removes and returns the item at position
sort none mutating Modifies a list to be sorted
reverse none mutating Modifies a list to be in reverse order
index item return idx Returns the position of first occurrence of item, error if not found
count item return ct Returns the number of occurrences of item
remove item mutating Removes the first occurrence of item
Here are some examples on these methods. Be sure to experiment with them to gain a better understanding of what they do. You are expected to be comfortable using these methods!
Another way to add items:
Some return type methods:
Some mutating methods:
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.
Some hybrid methods:
Notice that 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. Hybrid methods will also change the list without assignment to a variable, as shown with the last example above.
Details for these methods and others can be found in the Python Documentation
 1 
http://docs.python.org/py3k/library/stdtypes.html#sequence-types-str-bytes-bytearray-list-tuple-range
.
Check your understanding

Checkpoint 4.10.2.

    What is printed by the following statements?
    alist = [4, 2, 8, 6, 5]
    alist.append(True)
    alist.append(False)
    print(alist)
    
  • [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 4.10.3.

    What is printed by the following statements?
    alist = [4, 2, 8, 6, 5]
    alist.insert(2, True)
    alist.insert(0, False)
    print(alist)
    
  • [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 4.10.4.

    What is printed by the following statements?
    alist = [4, 2, 8, 6, 5]
    temp = alist.pop(2)
    temp = alist.pop()
    print(alist)
    
  • [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 4.10.5.

    What is printed by the following statements?
    alist = [4, 2, 8, 6, 5]
    alist = alist.pop(0)
    print(alist)
    
  • [2, 8, 6, 5]
  • alist 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 alist. The list is lost.
  • None
  • pop(0) returns the first item in the list so alist has now been changed.

Note 4.10.6.

This workspace is provided for your convenience. You can use this activecode window to try out anything you like.
You have attempted of activities on this page.