Start with the study notes on Lists & Tuples (definitions, examples and a quick revision), then work through 30 practice questions, each with the correct answer and a clear explanation of why it is right. When you are ready, take this topic as an interactive quiz to test yourself.
12 easy Β· 13 medium Β· 5 hard Β· Part of Python: Master Python from basics to advanced - 20 topics.
Topic overview
Lists and Tuples β ordered collections
1. Lists
A list is an ordered, changeable (mutable) collection, written in square brackets. It can hold any mix of types. Indexing and slicing work just like strings.
fruits = ["apple", "mango", "banana"]
print(fruits[0], fruits[-1])
print(fruits[1:])
print(len(fruits))
fruits[0] = "kiwi" # lists can be changed
print(fruits)
b = a does not copy a list. Both names refer to the same list, so a change through one shows through the other. Use a.copy(), a[:] or list(a) for an independent copy.
a = [1, 2, 3]
b = a
b.append(4)
print(a)
c = a.copy()
c.append(5)
print(a, c)
Output[1, 2, 3, 4]
[1, 2, 3, 4] [1, 2, 3, 4, 5]
6. Nested lists
A list can contain other lists, like the rows of a table. Index the row first, then the column.
matrix = [[1, 2],
[3, 4]]
print(matrix[1][0])
Output3
7. Tuples
A tuple is like a list, but immutable: it can't be changed after it is created. It is written with round brackets.
point = (3, 7)
x, y = point # unpacking
print(x, y)
print(type((5)), type((5,))) # the comma makes a tuple
Output3 7
<class 'int'> <class 'tuple'>
Remember:point[0] = 9 raises TypeError. A one-item tuple needs a comma: (5,).
When to use a tuple
Use a tuple for fixed data, such as coordinates or RGB colours. Because tuples can't change, they can be dictionary keys, while lists cannot.
Answer: B β Inserts 99 AT index 1 -> [10, 99, 20, 30]
insert(position, item) squeezes the item in at that index, shifting the rest right - nothing is overwritten: [10, 99, 20, 30].
insert()append()
Q6.medium
What is the difference between remove() and pop() on a list?
nums = [10, 20, 30]
nums.remove(20) # by value -> [10, 30]
last = nums.pop() # by position -> last = 30, nums = [10]
A No difference
B remove(x) deletes by VALUE; pop(i) deletes by POSITION and returns the item
C pop() deletes by value; remove() by position
D remove() empties the whole list
Answer: B β remove(x) deletes by VALUE; pop(i) deletes by POSITION and returns the item
nums.remove(20) searches for the value 20 and deletes its first occurrence. nums.pop(1) removes whatever sits at index 1 AND hands it back to you. pop() with no argument removes the last item.
remove()pop()del
Q9.medium
What is the output of:
nums = [5, 2, 8, 1]
nums.sort()
print(nums)
B list.sort() changes the original list; sorted() returns a NEW sorted list and works on any iterable
C sorted() changes the original; sort() returns new
D sorted() only works on numbers
Answer: B β list.sort() changes the original list; sorted() returns a NEW sorted list and works on any iterable
nums.sort() modifies nums itself (and returns None). sorted(anything) leaves the original untouched and returns a new sorted list - it even accepts strings, tuples and sets.
sort()sorted()
Q13.medium
What is the output of:
matrix = [[1, 2], [3, 4]]
print(matrix[1][0])
.index(value) returns the position of the first match only: 1. If the value isn't present it raises ValueError - check with in first.
index()count()
Q23.medium
What happens?
t = (1, 2, 3)
t[0] = 99
t = (1, 2, 3)
# t[0] = 99 # TypeError!
t = (99,) + t[1:] # build a new tuple
print(t) # (99, 2, 3)
A t becomes (99, 2, 3)
B TypeError - tuples don't support item assignment
C t becomes (99,)
D Nothing
Answer: B β TypeError - tuples don't support item assignment
Tuples are immutable - no assigning, appending or removing after creation. If you need to modify it, convert to a list first or build a new tuple.
ImmutabilityTypeError
Q25.medium
What does tuple unpacking do here?
point = (3, 7)
x, y = point
point = (3, 7)
x, y = point
print(x, y) # 3 7
A x = 3 and y = 7
B x = (3, 7) and y is undefined
C Error - tuples can't be split
D x = 7 and y = 3
Answer: A β x = 3 and y = 7
Unpacking assigns each tuple element to a variable, position by position: x gets 3, y gets 7. The variable count must match the element count.
UnpackingMultiple assignment
Q26.medium
When should you prefer a TUPLE over a list?
locations = {(0, 0): "origin", (3, 5): "target"} # tuple keys OK
# {[0, 0]: "x"} # TypeError - list keys not allowed
A For data that should never change - like coordinates, or dictionary keys
B When you need to add items later
C When sorting frequently
D Never - lists are always better
Answer: A β For data that should never change - like coordinates, or dictionary keys
Use tuples for fixed collections: a point (x, y), an RGB color, a date triple. Being immutable makes them safe from accidental changes and usable as dict keys (lists can't be keys).
ImmutabilityDictionary keys
Q28.medium
What is the output of:
nums = [1, 2, 3]
nums.reverse()
print(nums)
The del statement removes by position: del nums[1] leaves [10, 30]. del can also delete slices (del nums[1:3]) or the whole variable (del nums).
delpop()remove()
Hard questions (5)
Q10.hard
What is the output of:
nums = [3, 1, 2]
result = nums.sort()
print(result)
nums = [3, 1, 2]
print(nums.sort()) # None !
print(nums) # [1, 2, 3]
print(sorted([3,1,2])) # [1, 2, 3] - returns a NEW list
A [1, 2, 3]
B None
C [3, 1, 2]
D Error
Answer: B β None
Classic trap! .sort() sorts IN PLACE and returns None - the sorted data is in nums, not in result. Use sorted(nums) when you want the sorted list as a return value.
Rule: methods that modify in place (sort, append, reverse) return None.
sort() vs sorted()In-place methods
Q15.hard
What is the output?
a = [1, 2, 3]
b = a
b.append(4)
print(a)
a = [1, 2, 3]
b = a # same list, two names!
b.append(4)
print(a) # [1, 2, 3, 4]
c = a.copy() # independent copy
c.append(5)
print(a) # [1, 2, 3, 4] - unaffected
A [1, 2, 3]
B [1, 2, 3, 4]
C [4]
D Error
Answer: B β [1, 2, 3, 4]
b = a does NOT copy the list - both names point to the SAME list object. Appending through b changes the one shared list, so a shows [1, 2, 3, 4] too. To get an independent copy: b = a.copy() or b = a[:].
One of the most important Python concepts - assignment never copies.
Referencescopy()Aliasing
Q17.hard
What is the difference in result?
a = [1, 2]; a.append([3, 4])
versus
b = [1, 2]; b.extend([3, 4])
append() adds its argument as ONE item - even if it's a list, so you get a list inside a list. extend() unpacks the argument and adds each element individually.
append()extend()+= on lists
Q24.hard
What is the type of x?
x = (5)
a = (5)
print(type(a)) # <class 'int'>
b = (5,)
print(type(b)) # <class 'tuple'>
A tuple
B int - parentheses alone don't make a tuple; you need a comma: (5,)
C list
D str
Answer: B β int - parentheses alone don't make a tuple; you need a comma: (5,)
(5) is just the number 5 in parentheses - like in math. A one-element tuple needs a trailing COMMA: (5,). The comma makes the tuple, not the parentheses!
A famous Python gotcha - remember: the comma makes the tuple.
TuplesCommon gotchas
Q29.hard
What is the output of: print([0] * 3 == [0, 0, 0])
print([0] * 3) # [0, 0, 0]
grid = [[0] * 3 for _ in range(2)] # safe 2D grid
A True
B False
C [0, 0, 0]
D Error
Answer: A β True
[0] * 3 builds [0, 0, 0], and == on lists compares element by element - same length, same values -> True.
Careful: [[0]*3]*2 makes 2 references to the SAME row - use a comprehension for 2D grids.