Or as a one-liner:out[np.arange(len(x)), x] = 1
If NEP 21 is accepted (https://numpy.org/neps/nep-0021-advanced-indexing.html) this would be even simpler:out.vindex[:, x] = 1Was there ever a decision about that NEP? I didn't follow the discussion too closely at the time.