Numpy array: concatenate arrays and integers

arrays, concatenation, integer, numpy, python

Solution

They just have to be sequence objects, not necessarily numpy arrays:

x,y,z = 1,2,np.array([3,3,3])
np.concatenate(([x],[y],z))
# array([1, 2, 3, 4, 5])

Numpy also does have an `insert` function that will do this:

x,y,z = 1,2,np.array([3,3,3])
np.insert(z, [0,0], [x, y])

I'll add that if you're just trying to add integers to an list, you don't need numpy to do it:

x,y,z = 1,2,[3,3,3]
z = [x] + [y] + z

or

x,y,z = 1,2,[3,3,3]
[x, y] + z

or

x,y,z = 1,2,[3,3,3]
z.insert(0, y)
z.insert(0, x)

Problem

In my Python program I concatenate several integers and an array. It would be intuitive if this would work: ``` x,y,z = 1,2,np.array([3,3,3]) np.concatenate((x,y,z)) ``` However, instead all ints have to be converted to np.arrays: ``` x,y,z = 1,2,np.array([3,3,3]) np.concatenate((np.array([x]),np.array([y]),z)) ``` Especially if you have many variables this manual converting is tedious. The problem is that x and y are 0-dimensional arrays, while z is 1-dimensional. Is there any way to do the concatenation without the converting?

Original source