This is most likely a duplicate; apologies as I couldn't find the answer. I have a (3000, 3) array and I want to make a (3000, 4) array with the last column being a specific value. I can do a very simple np.insert(x, 1, val, axis=1) to insert the value before column 1, but I can't add it to the last column with np.insert(). I can do np.repeat(val, np.shape(x)[0]) or using np.hstack() but they require creating an array first of the same length as the original array (which varies) and a bit clunky (maybe this is the only way), and I'm guessing there is a better way if I am only concerned with one value I want to append.
Is there anything better than
np.concatenate((x, np.repeat(val, np.shape(x)[0])[:, np.newaxis]), axis=1)?
np.hstackmethod. It wouldn't really require any extra memory. But may I ask, if the value is the same for every row, why do you need this column in the array at all?(3000, 4), there is no way around allocating the memory for that extra column. So I'd suggest to just go withnp.hstack.