I'm trying to "overlay" a numpy 1-d array over another 1-d array of larger size, so that the overlaying array sets multiple values in the overlayed array. I'm struggling with an efficient way to convert the indices from the overlaying array (overlay) to the overlayed array (arr). overlay = [ 0 , 1 , 1 , 4 , 3] subixs = [[0, 1, 2, 3], [4, 5], [6], [7], 8] # relationship to `arr`, i.e. overlay[0] sets `arr[0]`, `arr[1]`, `arr[2]` and `arr[3]` assert len(overlay) == len(subixs) arr = [0, 0, 0, 0, 1, 1, 1, 4, 3] overlay[1] = 7 should turn arr to [0, 0, 0, 0, 7, 7, 1, 4, 3] because [1] refers to subixs[1] which in turn refers to arr[[4,5]]. This index conversion is easy for scalar indices, but for indices such as boolean arrays it becomes difficult. For instance, imagine: msk = (overlay == 1) | (overlay == 3) overlay[msk] = [44, 48, 47] in that case msk would need to be converted to [4, 5, 6, 8] to be used to the corresponding indices in arr and also the values to be set ([44, 48, 47]) would need to be expanded to [44, 44, 48, 47]. I figured out how to do the translation to the indices in arr, but it is rather inefficient as it's using lists and I also haven't figured out how to expand the values. new_value = [44, 48, 47] msk = (overlay == 1) | (overlay == 3) overlay[msk] = new_value # convert to indices in `arr` # Keep as list as numpy requires symmetric arrays subixs = [[0, 1, 2, 3], [4, 5], [6], [7], 8] subixs = np.asarray(subixs, dtype=object) arr_ixs = sum(subixs[msk], []) new_arr_value = ? # should be `[44, 44, 48, 47]` arr[arr_ixs] = new_arr_value How would I go about making the index translation more efficient (using numpy's vectorised functions) and expand new_value to new_arr_value? Edit I think I found a problem to expanding the values to fit arr. I can duplicate the values as many times as there are indices from one index in overlay pointing to arr. value_subixs = sub_ixs[msk] arr_ixs = sum(value_subixs, []) new_arr_value = np.repeat(value, np.asarray([len(ix) for ix in value_subixs])) arr[arr_ixs] = new_arr_value However, both solution (index translation and value expansion) are using lists which is not desirable as arr contains in the order of 100k elements. Continue reading...