def original(a, N):
    b = [[] for _ in range(N - 1)]
for indx, elem in enumerate(a):
    if 0 < elem < N:
        b[elem - 1].append(indx)
return b

def new(a, N):
    mask = (a > 0) & (a < N)
elements = a[mask]
indicies = np.arange(a.size)[mask]
return [indicies[elements == i] for i in range(1, N)]
Заполнение списка быстрее