Python Algorithm Cheatsheet

List

new list

lst = []

methods

lst.append(3)
lst.extend([5, 6])
last = lst.pop()      # last element is removed
count = lst.count(2)  # number of elements equal to 2
lst.sort(reverse=True)

others

# concatenate
[1, 2] + [3, 4]

slicing

lst[3:6] # from index 3 (included) to index 6 (not included)
lst[:4]  # from start to index 4 (not included)
lst[3:]  # from index 3 (included) to the end
lst[:]   # copy of the list
lst[:-1] # from start to the second last (included)

built-in functions for array