List Methods
Lists are mutable — you can add, remove, sort, and modify elements after creation. Python provides a rich set of built-in methods for manipulating lists efficiently.
Learning Objectives
- 11.2.3.3 Apply list methods
Conceptual Anchor
The Playlist Analogy
A list is like a music playlist. You can append a song at the end, insert one at a specific position, remove a song you don't like, sort alphabetically, or reverse the order. Unlike strings, lists are mutable — methods modify the original list directly.
Rules & Theory
Adding Elements
| Method | Description | Example | Result |
|---|---|---|---|
.append(x) |
Add x at the end | [1,2].append(3) |
[1, 2, 3] |
.insert(i, x) |
Insert x at index i | [1,3].insert(1, 2) |
[1, 2, 3] |
.extend(list) |
Add all elements from another list | [1,2].extend([3,4]) |
[1, 2, 3, 4] |
Removing Elements
| Method | Description | Example | Result |
|---|---|---|---|
.remove(x) |
Remove first occurrence of x | [1,2,3,2].remove(2) |
[1, 3, 2] |
.pop(i) |
Remove and return item at index i | [1,2,3].pop(1) |
returns 2, list: [1, 3] |
.pop() |
Remove and return last item | [1,2,3].pop() |
returns 3, list: [1, 2] |
.clear() |
Remove all elements | [1,2,3].clear() |
[] |
del lst[i] |
Delete item at index i | del lst[0] |
Removes first element |
Sorting & Searching
| Method | Description | Example |
|---|---|---|
.sort() |
Sort in-place (ascending) | [3,1,2].sort() → [1,2,3] |
.sort(reverse=True) |
Sort in-place (descending) | [1,3,2].sort(reverse=True) → [3,2,1] |
sorted(lst) |
Return new sorted list (original unchanged) | sorted([3,1,2]) → [1,2,3] |
.reverse() |
Reverse in-place | [1,2,3].reverse() → [3,2,1] |
.index(x) |
Index of first x | [10,20,30].index(20) → 1 |
.count(x) |
Count occurrences of x | [1,2,2,3].count(2) → 2 |
Built-in Functions for Lists
nums = [4, 2, 7, 1, 9, 3]
print(len(nums)) # 6 — number of elements
print(sum(nums)) # 26 — sum of all elements
print(min(nums)) # 1 — smallest element
print(max(nums)) # 9 — largest elementsort() vs sorted()
.sort() modifies the list in-place and returns None.
sorted() returns a new sorted list, leaving the original unchanged. Don't write
lst = lst.sort() — it sets lst to None!
Worked Examples
1 Student Grade Manager
grades = []
n = int(input("How many grades? "))
for i in range(n):
g = int(input(f"Grade {i+1}: "))
grades.append(g)
grades.sort()
print("Sorted:", grades)
print("Highest:", max(grades))
print("Lowest:", min(grades))
print("Average:", sum(grades) / len(grades))2 Stack with append/pop
# Lists can work as a stack (LIFO)
stack = []
stack.append("Page 1") # Push
stack.append("Page 2") # Push
stack.append("Page 3") # Push
print("Stack:", stack) # ['Page 1', 'Page 2', 'Page 3']
last = stack.pop() # Pop — returns "Page 3"
print("Popped:", last) # Page 3
print("Stack:", stack) # ['Page 1', 'Page 2']3 Removing Duplicates (Preserving Order)
nums = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
unique = []
for n in nums:
if n not in unique:
unique.append(n)
print("Original:", nums)
print("Unique:", unique) # [3, 1, 4, 5, 9, 2, 6]4 append() vs extend()
a = [1, 2, 3]
b = [4, 5, 6]
# append — adds the ENTIRE list as a single element
a_copy = a.copy()
a_copy.append(b)
print(a_copy) # [1, 2, 3, [4, 5, 6]] — nested list!
# extend — adds each element individually
a_copy2 = a.copy()
a_copy2.extend(b)
print(a_copy2) # [1, 2, 3, 4, 5, 6] — flat list ✓Pitfalls & Common Errors
lst = lst.sort() → None!
.sort() returns None because it modifies in-place. Writing
lst = lst.sort() overwrites lst with None. Just use
lst.sort() alone.
remove() Removes Only First Occurrence
[1, 2, 2, 3].remove(2) gives [1, 2, 3] — only the first 2 is removed.
To remove all, use a loop or list comprehension.
Modifying a List While Iterating
Removing elements during a for loop can skip elements. Iterate over a copy instead:
for x in lst.copy(): if ...: lst.remove(x)
Pro-Tips for Exams
List Method Cheat Sheet
- In-place (modify original):
append, insert, extend, remove, pop, sort, reverse, clear - Return new (don't modify):
sorted(), reversed(), len(), sum(), min(), max() pop()returns the removed element — useful for processing.index(x)raises ValueError if x not found — check withif x in lstfirst
Graded Tasks
Name 4 ways to add elements to a list and 4 ways to remove them.
Explain the difference between .sort() and sorted(). Why is
x = x.sort() a mistake?
Write a program that reads 10 numbers, removes the smallest and largest, and prints the average of the remaining.
Write a program that maintains a to-do list: add, remove, view, and sort tasks.
What is the state of lst after:
lst = [5,3,1,4,2]; lst.sort(); lst.insert(2, 10); lst.pop(0); del lst[-1]?
Implement a simple contact book that stores names in a sorted list and supports add, search, and delete operations.
Self-Check Quiz
.append(x) do?.remove(x) and
.pop(i)?[3,1,2].sort() return?.append([4,5]) and
.extend([4,5])?