How to use the insert() function in an array in Python

Overview

An array in Python is used to store multiple values of the same data type in a single variable.

The insert() function is used to insert an item into an array to any specified index position.

Syntax

insert(i, x)

Parameter values

Parameters

Description

i

This is a required parameter. It is the index position you want the item to be inserted in the array.

x

This is a required parameter. It is the value you want to insert into an array.

Implementation

Now, let’s create an array and insert a new item into the array:

# importing the array module
import array as arr
# creating an integer data type array
x = arr.array('i', [1,2,3,4,5,6,7])
# using the insert function
x.insert(3, 10)
# printing the new array
print(x)

Free Resources