def reverse_index(ls):
    """Reverse the order of indices in ls, which is a list of lists of the form
    ls[A][B]. Assumes the inner list has the same length for all A.
    Returns a list of lists where the corresponding element is ls[B][A].
    """
    rev = []
    if len(ls) == 0:
        return rev

    len_second = len(ls[0])
    for B in range(len_second):
        rev.append([])

    for ls_A in ls:
        for B, val_B in enumerate(ls_A):
            rev[B].append(val_B)

    return rev