Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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. Each data element in the array is identified by an index value. In Python, we can insert multiple elements at a specific index position using various approaches.
Arrays in Python
Python does not have a native array data structure, so we use the list data structure as an alternative ?
# Python list example my_list = [10, 4, 11, 76, 99] print(my_list)
[10, 4, 11, 76, 99]
We can also use the NumPy module to work with arrays ?
import numpy as np arr = np.array([1, 2, 3, 4]) print(arr)
[1 2 3 4]
The indexing in Python starts from 0, so the array elements are accessed using their respective index values like 0, 1, 2, up to n1.
Input Output Scenarios
Let's see some examples of inserting multiple elements at specified index positions ?
# Example 1 Input array: [9, 3, 7, 1] Output array: [9, 3, 6, 2, 10, 7, 1] # Elements 6, 2, 10 inserted at index 2 # Example 2 Input array: [2, 4, 6, 8, 1, 3, 9] Output array: [1, 1, 1, 2, 4, 6, 8, 1, 3, 9] # Elements 1, 1, 1 inserted at index 0
Using List Slicing
List slicing allows us to insert multiple elements at a specified index by assigning to an empty slice ?
numbers = [2, 3, 1, 4, 7, 5]
print("Original array:", numbers)
specified_index = 1
multiple_elements = [10, 11, 12]
# Insert elements using slice assignment
numbers[specified_index:specified_index] = multiple_elements
print("Array after inserting multiple elements:", numbers)
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
We can create a custom function using list slicing and concatenation to insert multiple elements at a specified position ?
def insert_elements(array, index, elements):
return array[:index] + elements + array[index:]
numbers = [1, 2, 3, 4, 5, 6]
print("Original array:", numbers)
specified_index = 2
multiple_elements = [10, 20, 30]
# Insert elements using custom function
result = insert_elements(numbers, specified_index, multiple_elements)
print("Array after inserting multiple elements:", result)
Original array: [1, 2, 3, 4, 5, 6] Array after inserting multiple elements: [1, 2, 10, 20, 30, 3, 4, 5, 6]
Using numpy.insert() Method
The numpy.insert() method provides a convenient way to insert multiple values at specified indices. The syntax is ?
numpy.insert(arr, obj, values, axis=None)
This method returns a copy of the input array with inserted values but does not modify the original array.
Example with Numbers
import numpy as np
arr = np.array([2, 4, 6, 8, 1, 3, 9])
print("Original array:", arr)
specified_index = 2
multiple_elements = [100, 200, 300]
# Insert elements using numpy.insert()
result = np.insert(arr, specified_index, multiple_elements)
print(f"Array after inserting elements at index {specified_index}:", result)
Original array: [2 4 6 8 1 3 9] Array after inserting elements at index 2: [ 2 4 100 200 300 6 8 1 3 9]
Example with Strings
import numpy as np
arr = np.array(['a', 'b', 'c', 'd'])
print("Original array:", arr)
specified_index = 0
multiple_elements = ['x', 'y', 'z']
# Insert string elements
result = np.insert(arr, specified_index, multiple_elements)
print(f"Array after inserting elements at index {specified_index}:", result)
Original array: ['a' 'b' 'c' 'd'] Array after inserting elements at index 0: ['x' 'y' 'z' 'a' 'b' 'c' 'd']
Comparison
| Method | Modifies Original? | Best For |
|---|---|---|
| List Slicing | Yes | Inplace modification of lists |
| List Concatenation | No | Creating new lists without modifying original |
| numpy.insert() | No | Working with NumPy arrays and numerical data |
Conclusion
Use list slicing for inplace insertion into Python lists. Use numpy.insert() for NumPy arrays and when working with numerical computations. Choose list concatenation when you need to preserve the original array.
