Python's list type provides a rich set of built-in methods for manipulating its contents in place. The most commonly used methods are: .append(x) (add one item to the end), .extend(iterable) (add all items from an iterable), .insert(i, x) (add an item at position i), .remove(x) (remove the first occurrence of value x, raises ValueError if not found), .pop(i) (remove and return the item at index i, defaulting to the last item), and .clear() (remove all items).
For searching and counting: .index(x) returns the index of the first occurrence of x (raises ValueError if not found), and .count(x) returns how many times x appears. For ordering: .sort() sorts the list in place (with optional key and reverse parameters), and .reverse() reverses the list in place. The built-in sorted() function returns a new sorted list, leaving the original unchanged.
A key distinction is between methods that modify the list in place and return None (like .sort(), .append(), .reverse()) and functions that return new lists (like sorted(), list concatenation with +, and list comprehensions). A common beginner mistake is writing new_list = old_list.sort(), which sets new_list to None because .sort() returns None — it sorts in place.
Discussed in:
- Chapter 6: Data Structures — Lists