Sitemap

Understanding Array Deletion in Python: Concept and Implementation

3 min readApr 3, 2025

--

Arrays are one of the fundamental data structures used in programming, providing a way to store multiple elements sequentially. However, manipulating arrays, such as deleting an element, requires an understanding of how elements are stored and shifted. In this article, we will explore the concept of array deletion in Python, understand the logic behind shifting elements, and implement a function to delete an element from an array.

Understanding Array Deletion

Unlike lists in Python, where we can directly remove an element using built-in functions like remove() or pop(), arrays (when used in a lower-level sense, such as fixed-size lists) require manual shifting of elements to maintain continuity. When an element is deleted from an array, the subsequent elements must be shifted to the left to fill the gap.

Step-by-Step Explanation

  1. Identify the position (index) of the element to delete.
  2. Shift all elements after the deleted element one position to the left.
  3. Decrease the size of the array to reflect the deletion.
  4. Print or return the updated array.

Implementing Array Deletion in Python

Below is a Python function that implements the deletion of an element from an array while maintaining its structure:

def delete_element(arr, size, position_to_delete):
if position_to_delete < 0 or position_to_delete >= size:
print("Invalid position!")
return size # No deletion performed

# Shift elements to fill the gap
for i in range(position_to_delete, size - 1):
arr[i] = arr[i + 1]

# Reduce the size of the array
size -= 1

# Print the updated array
for i in range(size):
print(arr[i], end=" ")
print()

return size # Return new size of the array

# Example usage
arr = [2, 4, 6, 8, 10]
size = 5
position_to_delete = 2 # Index of element to delete
size = delete_element(arr, size, position_to_delete)

Explanation of the Code

  1. Boundary Check: The function first checks if the given position is within a valid range.
  2. Shifting Elements: The loop shifts all elements from the given index one step to the left.
  3. Updating Size: The size is reduced by one to reflect the deletion.
  4. Printing the Updated Array: The modified array is printed after deletion.

Assume we have the following array :

Index: 0 1 2 3 4
Array: 2 4 6 8 10

Get Shruti Mandaokar’s stories in your inbox

Join Medium for free to get updates from this writer.

Now, if we want to delete the element at index 2 (which is 6), we need to shift all elements after it one position to the left:

Step-by-step shifting process

  1. Initial Array: [2, 4, 6, 8, 10]
  2. Shift index 8 to 2nd position. [2, 4, 8, 8, 10]
  3. Shift 10 to index 3 [2, 4, 8, 10, 10]
  4. Reduce Size [2, 4, 8, 10]

Before

Index: 0 1 2 3 4
Array: 2 4 6 8 10

After

Index: 0 1 2 3
Array: 2 4 8 10

Time Complexity Analysis

Since we iterate through the array to shift elements, the time complexity of this operation is O(n), where n is the number of elements in the array. The worst-case scenario occurs when deleting the first element, requiring all elements to be shifted.

Conclusion

Understanding how array deletion works is crucial for efficient programming, especially when working with fixed-size arrays in languages like C or Java. While Python lists provide built-in methods for deletion, manually handling the shifting process helps in understanding array behavior at a lower level. By implementing this function, you can efficiently remove elements and maintain the integrity of your array.

Happy Coding.

--

--

Shruti Mandaokar
Shruti Mandaokar

Written by Shruti Mandaokar

0 followers

Practicing Data Structures