Python lists come with built-in methods that let you add, remove, and rearrange elements without writing complex loops. For GCSE Computer Science, you are expected to use methods such as append(), remove(), sort(), and len() fluently in programs and to predict their output when tracing code.
What is a list method in Python?
A method is a function that belongs to a specific data type. List methods are called using dot notation: my_list.method_name(). This is different from a built-in function such as len(), which is called with the list passed as an argument: len(my_list).
Understanding the difference matters in exams:
scores = [7, 3, 9, 1]
scores.sort() # sort() is a list METHOD — called on the list
print(len(scores)) # len() is a FUNCTION — the list is passed in
How does append() work?
append(x) adds the value x to the end of the list. It modifies the list in place and returns None.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # Output: ['apple', 'banana', 'cherry']
fruits.append("date")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'date']
append() always adds to the end. To add at a specific position, use insert().
How does insert() work?
insert(i, x) adds the value x at index position i, shifting all existing elements to the right.
numbers = [10, 20, 40, 50]
numbers.insert(2, 30) # Insert 30 at index 2
print(numbers) # Output: [10, 20, 30, 40, 50]
Note: indices start at 0. Inserting at index 0 puts the item at the very beginning; inserting at an index beyond the end of the list appends it to the end.
How do remove() and pop() differ?
Both remove an element from a list, but in different ways:
| Method | Removes by | Returns | Raises error if |
|---|---|---|---|
remove(x) |
Value (first occurrence of x) | None | Value not found → ValueError |
pop(i) |
Index position i (default: last) | The removed value | Index out of range → IndexError |
colours = ["red", "green", "blue", "green"]
colours.remove("green") # Removes the FIRST "green"
print(colours) # Output: ['red', 'blue', 'green']
last = colours.pop() # Removes and returns the last item
print(last) # Output: 'green'
print(colours) # Output: ['red', 'blue']
colours.pop(0) # Removes item at index 0
print(colours) # Output: ['blue']
How do sort() and reverse() work?
sort() rearranges the list in ascending order (A to Z for strings, smallest to largest for numbers) in place — the original list is modified.
reverse() reverses the order of the list in place.
marks = [65, 42, 88, 73, 55]
marks.sort()
print(marks) # Output: [42, 55, 65, 73, 88]
marks.reverse()
print(marks) # Output: [88, 73, 65, 55, 42]
To sort in descending order directly, pass reverse=True to sort():
marks.sort(reverse=True)
print(marks) # Output: [88, 73, 65, 55, 42]
Important: sort() and reverse() both return None. A common error is writing marks = marks.sort(), which overwrites the list with None.
How do you check membership and find the length?
Two further operations that exams test frequently:
names = ["Alice", "Bob", "Carol"]
# Length
print(len(names)) # Output: 3
# Check if a value is present
print("Bob" in names) # Output: True
print("Dave" in names) # Output: False
# Find the index of a value
print(names.index("Carol")) # Output: 2
# Raises ValueError if not found
What common mistakes do GCSE students make with list methods?
| Mistake | Incorrect code | Correct code |
|---|---|---|
| Storing return of sort() | lst = lst.sort() |
lst.sort() |
| Removing by index with remove() | lst.remove(0) |
lst.pop(0) |
| Expecting remove() to remove all occurrences | lst.remove("x") removes all "x" |
Only removes the first; loop for all |
| Off-by-one with insert() | Confusing index 1 and 0 | Remember: index 0 = first position |
Frequently asked questions
What is the difference between sort() and sorted()?
sort() is a list method that modifies the list in place and returns None. sorted() is a built-in function that takes any iterable and returns a new sorted list, leaving the original unchanged. For GCSE, sort() is more commonly tested, but sorted() is useful when you need to keep the original order intact: sorted_marks = sorted(marks).
Can a Python list hold values of different data types?
Yes. Unlike arrays in some other languages, Python lists can hold a mix of integers, strings, floats, and even other lists. For example: mixed = [1, "hello", 3.14, True]. In GCSE work, lists usually hold values of a single consistent type to represent things like a class's exam scores or a register of names.
Does append() work the same way as concatenating with +?
No. list.append(x) adds a single item to an existing list in place (efficiently, in constant time). list + [x] creates a brand-new list containing all the original elements plus x — slower for large lists and requires reassignment (list = list + [x]). append() is nearly always the right choice for adding one item at a time inside a loop.
How do I remove all items from a list?
Use list.clear() to remove every element, leaving an empty list []. Alternatively, assigning list = [] creates a new empty list and points the variable at it, but any other variable that referenced the original list still sees the old contents. clear() modifies the list in place, so all references see the empty result.
Work through list-manipulation problems with instant hint-ladder feedback from Professor Turing at aitutors.me.