Unit 11.2A · Term 2

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

Lesson Presentation

11.2A-lesson-14-list-methods.pdf · Slides for classroom use

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 element

sort() 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 with if x in lst first

Graded Tasks

Remember

Name 4 ways to add elements to a list and 4 ways to remove them.

Understand

Explain the difference between .sort() and sorted(). Why is x = x.sort() a mistake?

Apply

Write a program that reads 10 numbers, removes the smallest and largest, and prints the average of the remaining.

Apply

Write a program that maintains a to-do list: add, remove, view, and sort tasks.

Analyze

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]?

Create

Implement a simple contact book that stores names in a sorted list and supports add, search, and delete operations.

Self-Check Quiz

1. What does .append(x) do?
Click to reveal answer
2. What is the difference between .remove(x) and .pop(i)?
Click to reveal answer
3. What does [3,1,2].sort() return?
Click to reveal answer
4. What is the difference between .append([4,5]) and .extend([4,5])?
Click to reveal answer
5. How do you find the position of value 30 in a list?
Click to reveal answer