def indices():
    for k in range(47):
        for j in range(1000):
            for i in range(40):
                yield k, j, i

def keyfn(ind):
    k, j, i = ind
return list_a[k][j][i]
print(nsmallest(4, indices(), key=keyfn))
Python - Finding elements in a list efficiently