Actively updating…

This post assume the reader to be familiar with at least one of other popular used programming language, preferably Java and C++; or know python before and just need refresh.


Python’s array type is similar to C++’s vector or Java’s ArrayList.

And for algorithm practice we usually use list, which will be introduced in next section.

Ref: array — Efficient arrays of numeric values

# create an array
array('u', 'hello \u2641')
array('l', [1, 2, 3, 4, 5])
array('d', [1.0, 2.0, 3.14])

# append value

# insert
array.insert(i, x)

# remove by index (i default to -1)

# remove by value

# Return the number of occurrences of x in the array.

# find first
array.index(x[, start[, stop]])

# reverse

Note: Numpy array is different. It’s not often used in algorithm problem solving though.


Ref: class list

Lists implement all of the common and mutable sequence operations.

# create a list
[a, b, c]
[x for x in iterable]

# Common sequence operations

x in s # True if an item of s is equal to x, else False

x not in s # False if an item of s is equal to x, else True

s + t # the concatenation of s and t

s * n or n * s # equivalent to adding s to itself n times

s[i] # ith item of s, origin 0

s[i:j] # slice of s from i to j

s[i:j:k] # slice of s from i to j with step k

len(s) # length of s

min(s) # smallest item of s

max(s) # largest item of s

s.index(x[, i[, j]]) # index of the first occurrence of x in s (at or after index i and before index j)

s.count(x) # total number of occurrences of x in s

# Mutable sequence operations

s[i] = x # item i of s is replaced by x

s[i:j] = t # slice of s from i to j is replaced by the contents of the iterable t

del s[i:j] # same as s[i:j] = []

s[i:j:k] = t # the elements of s[i:j:k] are replaced by those of t

del s[i:j:k] # removes the elements of s[i:j:k] from the list

s.append(x) # appends x to the end of the sequence (same as s[len(s):len(s)] = [x])

s.clear() # removes all items from s (same as del s[:])

s.copy() # creates a shallow copy of s (same as s[:])

s.extend(t) or s += t # extends s with the contents of t (for the most part the same as s[len(s):len(s)] = t)

s *= n # updates s with its contents repeated n times

s.insert(i, x) # inserts x into s at the index given by i (same as s[i:i] = [x])

s.pop() or s.pop(i) # retrieves the item at i and also removes it from s

s.remove(x) # remove the first item from s where s[i] is equal to x

s.reverse() # reverses the items of s in place

# sort a list

More about sort: Sorting HOW TO


Ref: class range

# class range(stop)
# class range(start, stop[, step])

# examples
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(1, 11))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> list(range(0, 30, 5))
[0, 5, 10, 15, 20, 25]
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> list(range(0, -10, -1))
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
>>> list(range(0))
>>> list(range(1, 0))


Ref: class str

# class str(object='')
# class str(object=b'', encoding='utf-8', errors='strict')

# Frequently used

str.count(sub[, start[, end]])

str.endswith(suffix[, start[, end]])

str.format(*args, **kwargs)

str.index(sub[, start[, end]])









str.replace(old, new[, count])

str.split(sep=None, maxsplit=- 1)

str.startswith(prefix[, start[, end]])



More about formatting: Format String Syntax





Priority queue


Numeric/Math module

Official docs

Python docs main page

Build-in Types

Build-in Functions