Python: Insert a new item before the second element in an existing array
10. Insert an Item Before the Second Element in an Array
Write a Python program to insert a newly created item before the second element in an existing array.
Pictorial Presentation:

Sample Solution:
Python Code :
from array import *
array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Insert new value 4 before 3:")
array_num.insert(1, 4)
print("New array: "+str(array_num))
Sample Output:
Original array: array('i', [1, 3, 5, 7, 9]) Insert new value 4 before 3: New array: array('i', [1, 4, 3, 5, 7, 9])
For more Practice: Solve these Related Problems:
- Write a Python program to insert an element at index 1 in an array using the insert() method.
- Write a Python program to implement a function that takes an array, a target index, and a value, then returns a new array with the value inserted before the target index.
- Write a Python program to split an array into two parts and insert a new element between them using slicing and concatenation.
- Write a Python program to insert an element at a specified position in an array and print the updated array.
Python Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Python program to append items from a specified list.
Next: Write a Python program to remove a specified item using the index from an array.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics