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.
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).
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.
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!
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.
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.