Python Program to insert multiple elements into the array from the specified index


An array is a collection of homogeneous data elements stored in an organized way. And each data element in the array is identified by an index value.

Arrays in python

Python does not have a native array data structure. So that, we can use the list data structure as an alternative to the arrays.

[10, 4, 11, 76, 99]

Also we can Python Numpy module to work with arrays.

An 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 different ways to insert multiple elements into the array at the specified index.

Input Output Scenarios

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

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

The elements 6, 2, 10 are inserted at the index position 2 and the element count is increased to 7.

Input arrays:
[2 4 6 8 1 3 9]
Output array:
[1 1 1 2 4  6 8 1 3 9]

Here the elements 1 1 1 are inserted at the 0th index position.

Using List slicing

To insert multiple elements at a specified index, we can use list slice.

Example

In this example, we will use the list slicing.

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

specified_index = 1
multiple_elements = 10, 11, 12

#  insert element
l[specified_index:specified_index] =  multiple_elements

print("Array after inserting multiple elements:", l)

Output

Original array: [2, 3, 1, 4, 7, 5]
Array after inserting multiple elements: [2, 10, 11, 12, 3, 1, 4, 7, 5]

Using List concatenation

Using list slicing and list concatenation we will create a function to insert multiple elements at a specified position. The Python list does not have any method to insert multiple elements at a specified position.

Example

Here we will define a function to insert multiple elements at a given index.

def insert_elements(array, index, elements):
    return array[:index] + elements + array[index:]

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

specified_index = 2
multiple_elements = list(range(1, 4))

# insert element
result = insert_elements(l, specified_index, multiple_elements)

print("Array after inserting multiple elements: ", result)

Output

Original array: [1, 2, 3, 4, 5, 6]
Array after inserting multiple elements: [1, 2, 1, 2, 3, 3, 4, 5, 6]

The insert_elements function inserted the elements ranging from 1 to 4 at the 2nd index position.

Using numpy.insert() method

In this example, we will use the numpy.insert() method to insert multiple values at the given indices. Following is the syntax −

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

The method returns a copy of the input array with inserted values. But it does not update the original array.

Example

In this example, we will use the numpy.insert() method to insert 3 elements at 2nd index position.

import numpy as np

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

specified_index = 2
multiple_elements = 1, 1, 1

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

print("Array {} after inserting multiple elements at the index {} ".format(result,specified_index))

Output

Original array:  [2 4 6 8 1 3 9]
Array [2 4 1 1 1 6 8 1 3 9] after inserting multiple elements at the index 2

The 3 elements 1, 1, 1 are inserted into the array arr at position 2 successfully.

Example

In this example, we will use the numpy array with all string elements.

import numpy as np
arr = np.array(['a','b', 'c', 'd'])
# print initial array
print("Original array: ", arr)
specified_index = 0
multiple_elements = list('ijk')
#  insert element
result = np.insert(arr, specified_index, multiple_elements)

print("Array {} after inserting multiple elements at the index {} ".format(result,specified_index))

Output

Original array:  ['a' 'b' 'c' 'd']
Array ['i' 'j' 'k' 'a' 'b' 'c' 'd'] after inserting multiple elements at the index 0

The elements 'i' 'j' 'k' are inserted at the 0th index position.

Updated on: 29-May-2023

918 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements