``````

In [1]:

import numpy as np

``````
``````

In [2]:

a = np.arange(4)
print(a)

``````
``````

[0 1 2 3]

``````
``````

In [3]:

print(np.insert(a, 2, 100))

``````
``````

[  0   1 100   2   3]

``````
``````

In [4]:

print(np.insert(a, 1, [100, 101, 102]))

``````
``````

[  0 100 101 102   1   2   3]

``````
``````

In [5]:

print(np.insert(a, [0, 2, 4], [100, 101, 102]))

``````
``````

[100   0   1 101   2   3 102]

``````
``````

In [6]:

_a = a.copy()
_a[1] = 100
print(_a)

``````
``````

[  0 100   2   3]

``````
``````

In [7]:

_a = a.copy()
_a[1:3] = [100, 101]
print(_a)

``````
``````

[  0 100 101   3]

``````
``````

In [8]:

# _a = a.copy()
# _a[1] = [100, 101, 102]
# print(_a)
# ValueError: setting an array element with a sequence.

``````
``````

In [9]:

_a = np.insert(a, 1, [100, 101, 102])
_a = np.delete(_a, 4)
print(_a)

``````
``````

[  0 100 101 102   2   3]

``````
``````

In [10]:

a = np.arange(12).reshape((3, 4))
print(a)

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

``````
``````

In [11]:

print(np.insert(a, 2, 100))

``````
``````

[  0   1 100   2   3   4   5   6   7   8   9  10  11]

``````
``````

In [12]:

print(np.insert(a, 2, 100, axis=0))

``````
``````

[[  0   1   2   3]
[  4   5   6   7]
[100 100 100 100]
[  8   9  10  11]]

``````
``````

In [13]:

b1 = np.arange(100, 104)
print(b1)

``````
``````

[100 101 102 103]

``````
``````

In [14]:

print(np.insert(a, 1, b1, axis=0))

``````
``````

[[  0   1   2   3]
[100 101 102 103]
[  4   5   6   7]
[  8   9  10  11]]

``````
``````

In [15]:

print(np.insert(a, 3, b1, axis=0))

``````
``````

[[  0   1   2   3]
[  4   5   6   7]
[  8   9  10  11]
[100 101 102 103]]

``````
``````

In [16]:

print(np.insert(a, [0, 2], b1, axis=0))

``````
``````

[[100 101 102 103]
[  0   1   2   3]
[  4   5   6   7]
[100 101 102 103]
[  8   9  10  11]]

``````
``````

In [17]:

b2 = np.arange(100, 112).reshape((3, 4))
print(b2)

``````
``````

[[100 101 102 103]
[104 105 106 107]
[108 109 110 111]]

``````
``````

In [18]:

print(np.insert(a, 2, b2, axis=0))

``````
``````

[[  0   1   2   3]
[  4   5   6   7]
[100 101 102 103]
[104 105 106 107]
[108 109 110 111]
[  8   9  10  11]]

``````
``````

In [19]:

print(np.insert(a, 2, b2[2], axis=0))

``````
``````

[[  0   1   2   3]
[  4   5   6   7]
[108 109 110 111]
[  8   9  10  11]]

``````
``````

In [20]:

print(np.insert(a, [0, 2, 3], b2, axis=0))

``````
``````

[[100 101 102 103]
[  0   1   2   3]
[  4   5   6   7]
[104 105 106 107]
[  8   9  10  11]
[108 109 110 111]]

``````
``````

In [21]:

print(np.insert(a, range(3), b2, axis=0))

``````
``````

[[100 101 102 103]
[  0   1   2   3]
[104 105 106 107]
[  4   5   6   7]
[108 109 110 111]
[  8   9  10  11]]

``````
``````

In [22]:

print(np.vstack((a, b1)))

``````
``````

[[  0   1   2   3]
[  4   5   6   7]
[  8   9  10  11]
[100 101 102 103]]

``````
``````

In [23]:

print(np.vstack((b2, a)))

``````
``````

[[100 101 102 103]
[104 105 106 107]
[108 109 110 111]
[  0   1   2   3]
[  4   5   6   7]
[  8   9  10  11]]

``````
``````

In [24]:

_a = a.copy()
_a[2] = b1
print(_a)

``````
``````

[[  0   1   2   3]
[  4   5   6   7]
[100 101 102 103]]

``````
``````

In [25]:

_a = a.copy()
_a[1] = b2[1]
print(_a)

``````
``````

[[  0   1   2   3]
[104 105 106 107]
[  8   9  10  11]]

``````
``````

In [26]:

_a = a.copy()
_a[1:] = b2[[0, 2]]
print(_a)

``````
``````

[[  0   1   2   3]
[100 101 102 103]
[108 109 110 111]]

``````
``````

In [27]:

print(a)

``````
``````

[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]]

``````
``````

In [28]:

print(np.insert(a, 1, 100, axis=1))

``````
``````

[[  0 100   1   2   3]
[  4 100   5   6   7]
[  8 100   9  10  11]]

``````
``````

In [29]:

c1 = np.arange(100, 103)
print(c1)

``````
``````

[100 101 102]

``````
``````

In [30]:

print(np.insert(a, 1, c1, axis=1))

``````
``````

[[  0 100   1   2   3]
[  4 101   5   6   7]
[  8 102   9  10  11]]

``````
``````

In [31]:

print(np.insert(a, 3, c1, axis=1))

``````
``````

[[  0   1   2 100   3]
[  4   5   6 101   7]
[  8   9  10 102  11]]

``````
``````

In [32]:

c2 = np.arange(100, 106).reshape((3, 2))
print(c2)

``````
``````

[[100 101]
[102 103]
[104 105]]

``````
``````

In [33]:

# print(np.insert(a, 1, c2, axis=1))
# ValueError: could not broadcast input array from shape (2,3) into shape (3,3)

``````
``````

In [34]:

print(np.insert(a, [1], c2, axis=1))

``````
``````

[[  0 100 101   1   2   3]
[  4 102 103   5   6   7]
[  8 104 105   9  10  11]]

``````
``````

In [35]:

print(np.insert(a, [0, 2], c2, axis=1))

``````
``````

[[100   0   1 101   2   3]
[102   4   5 103   6   7]
[104   8   9 105  10  11]]

``````
``````

In [36]:

print(c1)

``````
``````

[100 101 102]

``````
``````

In [37]:

print(np.insert(a, 1, c1, axis=1))

``````
``````

[[  0 100   1   2   3]
[  4 101   5   6   7]
[  8 102   9  10  11]]

``````
``````

In [38]:

print(np.insert(a, [1], c1, axis=1))

``````
``````

[[  0 100 101 102   1   2   3]
[  4 100 101 102   5   6   7]
[  8 100 101 102   9  10  11]]

``````
``````

In [39]:

print(np.insert(a, [1, 3, 4], c1, axis=1))

``````
``````

[[  0 100   1   2 101   3 102]
[  4 100   5   6 101   7 102]
[  8 100   9  10 101  11 102]]

``````
``````

In [40]:

_c1 = c1.reshape((3, 1))
print(_c1)

``````
``````

[[100]
[101]
[102]]

``````
``````

In [41]:

print(np.insert(a, 1, _c1, axis=1))

``````
``````

[[  0 100 101 102   1   2   3]
[  4 100 101 102   5   6   7]
[  8 100 101 102   9  10  11]]

``````
``````

In [42]:

print(np.insert(a, [1], _c1, axis=1))

``````
``````

[[  0 100   1   2   3]
[  4 101   5   6   7]
[  8 102   9  10  11]]

``````
``````

In [43]:

print(np.insert(a, [1, 3, 4], _c1, axis=1))

``````
``````

[[  0 100   1   2 100   3 100]
[  4 101   5   6 101   7 101]
[  8 102   9  10 102  11 102]]

``````
``````

In [44]:

# print(np.hstack((a, c1)))
# ValueError: all the input arrays must have same number of dimensions

``````
``````

In [45]:

print(_c1)

``````
``````

[[100]
[101]
[102]]

``````
``````

In [46]:

print(np.hstack((a, _c1)))

``````
``````

[[  0   1   2   3 100]
[  4   5   6   7 101]
[  8   9  10  11 102]]

``````
``````

In [47]:

print(np.hstack((_c1, a)))

``````
``````

[[100   0   1   2   3]
[101   4   5   6   7]
[102   8   9  10  11]]

``````
``````

In [48]:

print(np.hstack((a, c2)))

``````
``````

[[  0   1   2   3 100 101]
[  4   5   6   7 102 103]
[  8   9  10  11 104 105]]

``````
``````

In [49]:

print(np.hstack((c2, a)))

``````
``````

[[100 101   0   1   2   3]
[102 103   4   5   6   7]
[104 105   8   9  10  11]]

``````
``````

In [50]:

_a = a.copy()
_a[:, 1] = c1
print(_a)

``````
``````

[[  0 100   2   3]
[  4 101   6   7]
[  8 102  10  11]]

``````
``````

In [51]:

_a = a.copy()
_a[:, :2] = c2
print(_a)

``````
``````

[[100 101   2   3]
[102 103   6   7]
[104 105  10  11]]

``````
``````

In [52]:

_a = a.copy()
_a[:, [0, 3]] = c2
print(_a)

``````
``````

[[100   1   2 101]
[102   5   6 103]
[104   9  10 105]]

``````