Adding Elements
lst = [1, 2, 3] lst.append(4) # [1,2,3,4] lst.extend([5,6]) # [1,2,3,4,5,6] lst.insert(0, 0) # [0,1,2,3,4,5,6]
Removing
lst.pop() # remove & return last lst.pop(0) # remove & return index 0 lst.remove(3) # remove first matching value lst.clear() # empty the list
Searching
lst.index(5) # position of 5 (ValueError if missing) lst.count(2) # how many 2s 2 in lst # membership test
Ordering
lst.sort() # in place lst.sort(reverse=True) lst.sort(key=lambda x: -x) lst.reverse() sorted(lst) # returns new list
Copying
a = [1,2,3] b = a # same list! c = a[:] # shallow copy d = list(a) # shallow copy import copy e = copy.deepcopy(a)
Practise this on PyForm — free
PyForm runs Python in your browser with an AI tutor trained for HKDSE. No install, no credit card.
Open PyForm →