Python Program to insert an element into the array at the specified index


An array is a data structure, which is used to store the collection of homogeneous data elements. And each element in the array is identified by an index value.

Arrays in python

Python does not have its own data structure to represent an array. However, we can use the list data structure as an alternative to the arrays. Here we will use list an array −

[10, 4, 11, 76, 99]

Python provides some modules also which are more appropriate, and the modules are Numpy and array modules.

A Numpy array defined by the NumPy module is −

array([1, 2, 3, 4])

The indexing in python starts from 0, so that the above array elements are accessed using their respective index values like 0, 1, 2, upto n-1.

In the article below, we see multiple ways to insert an element into the array at the specified index using Python programming.

Input Output Scenarios

Assume we have an array A with 6 integer values. And the resultant array will have inserted element at a specified index position.

Input array:
[9, 3, 6, 2, 7, 1]
Output array:
[9, 3, 6, 2, 10, 7, 1]

The element 10 is inserted at the index position 4 and the elements count is increased to 7 after inserting a value. Let us look into the another array.

Input arrays:
[‘a’, ‘b’, ‘c’]
Output array:
[‘k’, ‘a’, ‘b’, ‘c’]

Here the elements ‘k’ is inserted at the first index.

Using the list.insert() method

To insert an element into the array at the specified index, we can use the insert() method in python. The insert() is a list data structures built-in method that is used to insert an element at a given index. Following is the syntax of this function –

list.insert(index, element)

The index parameter specifies where to insert the element, and the second parameter is the given element. The method list.insert() does not return anything it updates the original list.

Example

In this example we will use the list.insert() method.

l = [9, 3, 6, 2, 7, 1]
# print initial array
print("Original array: ", l)

specified_index = 4
element = 10

#  insert element
l.insert(specified_index, element)

print("Array after inserting the element: ", l)

Output

Original array:  [9, 3, 6, 2, 7, 1]
Array after inserting the element:  [9, 3, 6, 2, 10, 7, 1]

The original array l is updated with the new element 10 at the given position 4.

Using for loop

Without using any method/function we can insert a value into an array using for loop.

Example

Here we will use the python for loop to insert a value at particular position.

l = [3, 6, 2, 8, 4]
# print initial array
print("Original array: ", l)
specified_index = 1
element = 0

for i in range(len(l)):
   if i == specified_index:
      l[i] = element

print("Array after inserting the element: ", l)

Output

Original array:  [3, 6, 2, 8, 4]
Array after inserting the element:  [3, 0, 2, 8, 4]

Here we iterated the for loop using the array index values and compared the index with the specified index, then inserted the element where the index is matched with the specified index.

Using the numpy.insert() method

The numpy.insert() method is used to insert a value at the given indices. And it returns a copy of the input array with inserted values. Following is the syntax –

numpy.insert(arr, obj, values, axis=None)

Parameters

  • arr − input NumPy array.

  • obj − it defines the index where to insert the element.

  • values − elements to insert into the input array.

  • axis − along which to insert values. By default, None.

Example

In this example, we wil use the numpy.insert() method.

import numpy as np

arr = np.array([0, 6, 2, 7, 9, 1, 4])
# print initial array
print("Original array: ", arr)

specified_index = 5
element = 10

#  insert element
result = np.insert(arr, specified_index, element)

print("Array after inserting the element: ", result)

Output

Original array:  [0 6 2 7 9 1 4]
Array after inserting the element:  [ 0  6  2  7  9 10  1  4]

The element 10 is successfully inserted at the position 5 using the numpy.insert() function.

Updated on: 29-May-2023

562 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements