Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array
python
communitycreator

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

Onyejiaku Theophilus Chidalu

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)
Insert an item in a specific location in an array

RELATED TAGS

array
python
communitycreator

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring