Python List Methods
List Satellite
List Methods
Lists come with a toolbox for mutating, searching, and rearranging data. Keep this reference handy.
Method table
| Method | Description |
|---|---|
| append(x) | Add item to end |
| extend(iterable) | Concatenate iterables |
| insert(i, x) | Insert at index (O(n)) |
| remove(x) | Remove first occurrence |
| pop([i]) | Remove and return element |
| clear() | Remove all elements |
| index(x) | Find first index of value |
| count(x) | Count occurrences |
| sort() | In-place sort |
| reverse() | Reverse in place |
| copy() | Shallow copy |
Tips
- Use
sorted(items)when you need a new list. - Combine
extendwith generator expressions for streaming data. - Prefer
collections.dequefor repeated pops from the left.