Python Lists: Slicing, Methods, and Comprehensions for Exams
Lists are the most-tested Python data structure. Here's what exams focus on — slicing syntax, mutating methods, and comprehensions.
Creating lists and accessing elements
Lists are ordered, mutable sequences. Indexing starts at 0. Negative indexes count from the end.
items = [10, 20, 30, 40, 50] print(items[0]) # 10 (first) print(items[-1]) # 50 (last) print(items[-2]) # 40 (second from last)
Slicing — the syntax that trips everyone up
Slice syntax is `list[start:stop:step]`. The stop index is excluded. Omitting start defaults to 0; omitting stop defaults to the end of the list.
nums = [0, 1, 2, 3, 4, 5] print(nums[1:4]) # [1, 2, 3] — stop is excluded print(nums[:3]) # [0, 1, 2] print(nums[2:]) # [2, 3, 4, 5] print(nums[::2]) # [0, 2, 4] — every 2nd item print(nums[::-1]) # [5, 4, 3, 2, 1, 0] — reversed
Key list methods
Know which methods modify the list in place and which return a new value.
lst = [3, 1, 4, 1, 5] lst.append(9) # adds to end — returns None lst.extend([2, 6]) # adds multiple items — returns None lst.insert(0, 0) # insert at index — returns None lst.pop() # removes and returns last item lst.pop(0) # removes and returns item at index lst.remove(1) # removes first occurrence of value lst.sort() # sorts in place — returns None new = sorted(lst) # returns new sorted list lst.reverse() # reverses in place — returns None
List comprehensions
A list comprehension builds a new list in a single readable expression. It can include a filter condition.
# Basic squares = [x**2 for x in range(5)] # [0, 1, 4, 9, 16] # With filter evens = [x for x in range(10) if x % 2 == 0] # [0, 2, 4, 6, 8] # Nested matrix = [[i * j for j in range(3)] for i in range(3)] # [[0,0,0], [0,1,2], [0,2,4]]
Shallow vs deep copy
Assigning a list to a new variable doesn't copy it — both variables point to the same object.
a = [1, 2, 3] b = a # same object b.append(4) print(a) # [1, 2, 3, 4] — a was modified! c = a.copy() # shallow copy c.append(5) print(a) # [1, 2, 3, 4] — a unchanged
Exam tip
Slice notation is the single most-tested list topic. Practice `[start:stop:step]` until `nums[::-1]` and `nums[1:4]` are instinctive. Also memorise that sort() returns None.
Think you're ready? Prove it.
Take the free Python readiness test. Get a score, topic breakdown, and your exact weak areas.
Take the free Python test →Free · No sign-up · Instant results