# Numpy: How to permute array into indices of larger array? [closed]

I have an array of length L with N zeros, and L-N non-zero values. I have another array of length N. I would like to put the values of the shorter array into the positions of the longer array which are zero, keeping them in order. For example, if I had,

$$L = \, [1,2,3,0,0,4,5,6,0]$$ $$N = \, [10,0,20]$$ I would want, $$L' = \, [1,2,3,10,0,4,5,6,20]$$ I will do this repeatedly with the same $$L$$ and many different $$N$$ (all of the same length). What is the best way to accomplish this using Numpy?

L[L==0]=N


Unless I’m missing something in your question. In numpy the main constraint is that you want to work with built-in array objects as much as possible. But taking a subset of an array, with indices given by a logical array, is a built-in operation so I expect this to be sufficient. This modifies L in-place, so you’ll need to create a copy for every new N.