DEV Community

Cover image for Array Manipulation: A Deep Dive into Insertions and Deletions
Lohith
Lohith

Posted on • Updated on

Array Manipulation: A Deep Dive into Insertions and Deletions

When you need to insert or remove one or more elements in an array, you can use the following functions to accomplish these tasks:


numpy.append is a function in the NumPy library, which is used to append values to the end of an array. It takes the array to which you want to append values, the values you want to append, and an axis parameter (optional) which specifies the axis along which the values are appended. If the axis parameter is not specified, the array is flattened before appending.

Here's the syntax:

numpy.append(array, values, axis=None)
Enter fullscreen mode Exit fullscreen mode
  • array: The array to which you want to append values.
  • values: The values you want to append to the array.
  • axis (optional): The axis along which the values are appended. If not provided, the array is flattened before appending.

Here's an example:

import numpy as np

# Create a numpy array
arr = np.array([1, 2, 3])

# Append a single value to the end of the array
arr = np.append(arr, 4)
print(arr)  

# Append multiple values to the end of the array
arr = np.append(arr, [5, 6, 7])
print(arr)  

# Append values along a specific axis
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Original array:")
print(arr)

# Append a row at the end of the array
arr = np.append(arr, [[7, 8, 9]], axis=0)
print("Appended along axis 0:")
print(arr)

# Append a column at the end of the array
arr = np.append(arr, [[10], [11], [12]], axis=1)
print("Appended along axis 1:")
print(arr)
Enter fullscreen mode Exit fullscreen mode
# Append a single value to the end of the array
 Output: [1 2 3 4]

# Append multiple values to the end of the array
 Output: [1 2 3 4 5 6 7]

# Append values along a specific axis
 Output:
 [[1 2 3]
  [4 5 6]]

# Append a row at the end of the array
 Output:
 [[1 2 3]
  [4 5 6]
  [7 8 9]]

# Append a column at the end of the array
 Output:
 [[ 1  2  3 10]
  [ 4  5  6 11]
  [ 7  8  9 12]]
Enter fullscreen mode Exit fullscreen mode

In this example, we first create a numpy array arr. Then, we use np.append() to append values to this array. We append single values, multiple values, and also append values along specific axes by specifying the axis parameter.


numpy.insert() is a function in the NumPy library used to insert elements into an array along a specified axis. It takes the following syntax:

numpy.insert(arr, obj, values, axis=None)
Enter fullscreen mode Exit fullscreen mode
  • arr: The input array.
  • obj: The index or indices before which values should be inserted. This can be an integer, a sequence of integers, or a slice object.
  • values: The scalar or array-like values to insert into arr.
  • axis: The axis along which to insert values. If not specified, arr is flattened before insertion.

Here's an example to illustrate how numpy.insert() works:

import numpy as np

# Create an array
arr = np.array([[1, 2], [3, 4], [5, 6]])

# Insert a value at index 1 along axis 0 (rows)
new_arr = np.insert(arr, 1, [7, 8], axis=0)

print(new_arr)
Enter fullscreen mode Exit fullscreen mode

Output:

[[1 2]
 [7 8]
 [3 4]
 [5 6]]
Enter fullscreen mode Exit fullscreen mode

In this example, [7, 8] is inserted before the row at index 1 along axis 0. So, it becomes the second row in the resulting array.


numpy.delete is a function in the NumPy library for Python used to delete elements from an array along a specified axis. It returns a new array with the specified elements removed.

Here's the syntax:

numpy.delete(arr, obj, axis=None)
Enter fullscreen mode Exit fullscreen mode
  • arr: The input array.
  • obj: An index or a slice representing the elements to delete.
  • axis: The axis along which to delete the specified elements. If not provided, the input array is flattened before deletion.

Here's an example:

import numpy as np

# Create a sample array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Delete the first row (index 0) along axis 0
new_arr = np.delete(arr, 0, axis=0)

print("Original array:")
print(arr)
print("Array after deleting the first row:")
print(new_arr)
Enter fullscreen mode Exit fullscreen mode

Output:

Original array:
[[1 2 3]
 [4 5 6]
 [7 8 9]]
Array after deleting the first row:
[[4 5 6]
 [7 8 9]]
Enter fullscreen mode Exit fullscreen mode

In this example, np.delete(arr, 0, axis=0) deletes the first row (index 0) along the vertical axis (axis=0) of the array arr, resulting in new_arr.

Top comments (0)