In Python an array can be either of type list or of type tuple with the constraint that it is immutable. One of the key features of a list is that it can be dynamically resized, values can be deleted or inserted at arbitrary locations in the list.

  • Instantiate:
    • A = [3, 5, 8, 13]
    • A = [1] + [0] * 10
    • A = list(range(10))
    • A = [[1, 2, 4], [3, 5, 7, 9], [13]] for 2D array
  • Basic operations:
    • len(A)
    • A.append(42)
    • A.remove(2)
    • A.insert(3, 28)
  • Check if a value is in an array:
    • if a in A:
  • Copy:
    • B = A vs. B = list(A)
    • copy.copy(A) vs. copy.deepcopy(A)
  • Methods:
    • min(A)
    • max(A)
    • binary search for sorted lists:
      • bisect.bisect(A, 6)
      • bisect.bisect_left(A, 6)
      • bisect.bisect_right(A, 6)
    • A.reverse()(in-place) vs. reversed(A)(returns an iterator)
    • A.sort()(in-place) vs. sorted(A)(returns a copy),
    • del A[i](deletes the i-th element)
    • del A[i:j](removes the slice)
  • Slicing - given an array A = [1, 6, 3, 4, 5, 2, 7]:
    • A[2: 4] returns [3, 4]
    • A[2:] returns [3, 4, 5, 2, 7]
    • A[:4] returns [1, 6, 3, 4]
    • A[:-1] returns [1, 6, 3, 4, 5, 2]
    • A[-3:] returns [5, 2, 7]
    • A[-3: -1] returns [5, 2]
    • A[1: 5: 2] returns [6, 4]
    • A[5: 1: -2] returns [2, 4]
    • A[::-1] returns [7, 2, 5, 4, 3, 6, 1](reverses list)
  • List Comprehension: An in-depth overview can be found here, written by Steven Rosa.