Python Array insert() Method

The insert() method in Python is used to insert an element at a specified position in an array. This method allows you to add an element at any position within the array, shifting the elements to the right to make room for the new element.

Table of Contents

  1. Introduction
  2. Importing the array Module
  3. insert() Method Syntax
  4. Understanding insert()
  5. Examples
    • Basic Usage
    • Inserting at the Beginning
    • Inserting at the End
  6. Real-World Use Case
  7. Conclusion

Introduction

The insert() method is a built-in method for array objects in Python, which are provided by the array module. This method allows you to add an element at a specific position within the array.

Importing the array Module

Before using the insert() method, you need to import the array module, which provides the array object.

import array

insert() Method Syntax

The syntax for the insert() method is as follows:

array.insert(i, x)

Parameters:

  • i: The index at which to insert the element.
  • x: The element to be inserted.

Returns:

  • None. The method modifies the array in place.

Understanding insert()

The insert() method inserts an element at the specified index in the array. The elements to the right of the specified index are shifted to the right to make room for the new element.

Examples

Basic Usage

To demonstrate the basic usage of insert(), we will create an array and insert an element at a specified position.

Example

import array

# Creating an array of integers
arr = array.array('i', [1, 2, 3, 4, 5])

# Inserting an element at index 2
arr.insert(2, 10)

# Printing the array
print("Array after insertion:", arr)

Output:

Array after insertion: array('i', [1, 2, 10, 3, 4, 5])

Inserting at the Beginning

This example shows how to insert an element at the beginning of an array.

Example

import array

# Creating an array of integers
arr = array.array('i', [1, 2, 3, 4, 5])

# Inserting an element at the beginning (index 0)
arr.insert(0, 99)

# Printing the array
print("Array after inserting at the beginning:", arr)

Output:

Array after inserting at the beginning: array('i', [99, 1, 2, 3, 4, 5])

Inserting at the End

This example shows how to insert an element at the end of an array by specifying the index equal to the length of the array.

Example

import array

# Creating an array of integers
arr = array.array('i', [1, 2, 3, 4, 5])

# Inserting an element at the end
arr.insert(len(arr), 77)

# Printing the array
print("Array after inserting at the end:", arr)

Output:

Array after inserting at the end: array('i', [1, 2, 3, 4, 5, 77])

Real-World Use Case

Maintaining Sorted Lists

In real-world applications, the insert() method can be used to maintain sorted lists by inserting new elements at the correct position.

Example

import array

# Function to insert an element into a sorted array
def insert_sorted(arr, element):
    for i in range(len(arr)):
        if arr[i] > element:
            arr.insert(i, element)
            return
    arr.append(element)

# Creating a sorted array of integers
sorted_arr = array.array('i', [10, 20, 30, 40, 50])

# Inserting elements while maintaining the sorted order
insert_sorted(sorted_arr, 25)
insert_sorted(sorted_arr, 5)
insert_sorted(sorted_arr, 55)

# Printing the sorted array
print("Sorted array after insertions:", sorted_arr)

Output:

Sorted array after insertions: array('i', [5, 10, 20, 25, 30, 40, 50, 55])

Adding Elements to a Priority Queue

The insert() method can also be used to add elements to a priority queue at the correct position based on priority.

Example

import array

# Creating a priority queue array (lower number indicates higher priority)
priority_queue = array.array('i', [1, 3, 5, 7])

# Function to insert an element into the priority queue
def insert_with_priority(queue, element):
    for i in range(len(queue)):
        if queue[i] > element:
            queue.insert(i, element)
            return
    queue.append(element)

# Inserting elements with priority
insert_with_priority(priority_queue, 2)
insert_with_priority(priority_queue, 6)
insert_with_priority(priority_queue, 0)

# Printing the priority queue
print("Priority queue after insertions:", priority_queue)

Output:

Priority queue after insertions: array('i', [0, 1, 2, 3, 5, 6, 7])

Conclusion

The insert() method in Python is used to insert an element at a specified position in an array. This method is useful for maintaining ordered lists, adding elements to specific positions, and managing priority queues.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare