0
0
DSA Pythonprogramming

Array Deletion at Beginning in DSA Python

Choose your learning style9 modes available
Mental Model
Removing the first item from a list means shifting all other items one step to the left.
Analogy: Imagine a line of people waiting to buy tickets. When the first person leaves, everyone else moves forward one step to fill the empty space.
Index:  [0] -> [1] -> [2] -> [3] -> null
Value:  10  ->  20  ->  30  ->  40  -> null
Dry Run Walkthrough
Input: array: [10, 20, 30, 40], delete element at beginning
Goal: Remove the first element and shift all others left to keep order
Step 1: Remove element at index 0 (value 10)
Index:  [0] -> [1] -> [2] -> [3] -> null
Value:  10  ->  20  ->  30  ->  40  -> null
Why: The first element is removed logically by shifting elements
Step 2: Shift element at index 1 (20) to index 0
Index:  [0] -> [1] -> [2] -> [3] -> null
Value:  20  ->  20  ->  30  ->  40  -> null
Why: Elements move left to fill the empty space at the start
Step 3: Shift element at index 2 (30) to index 1
Index:  [0] -> [1] -> [2] -> [3] -> null
Value:  20  ->  30  ->  30  ->  40  -> null
Why: Continue shifting elements left to maintain order
Step 4: Shift element at index 3 (40) to index 2
Index:  [0] -> [1] -> [2] -> [3] -> null
Value:  20  ->  30  ->  40  ->  40  -> null
Why: Last element moves left to fill the last empty spot
Step 5: Reduce array size by one, removing last duplicate
Index:  [0] -> [1] -> [2] -> null
Value:  20  ->  30  ->  40  -> null
Why: Array length decreases to reflect deletion
Result:
Index:  [0] -> [1] -> [2] -> null
Value:  20  ->  30  ->  40  -> null
Annotated Code
DSA Python
class Array:
    def __init__(self, elements):
        self.arr = elements
        self.size = len(elements)

    def delete_at_beginning(self):
        if self.size == 0:
            return  # Nothing to delete
        for i in range(1, self.size):
            self.arr[i - 1] = self.arr[i]  # Shift element left
        self.size -= 1  # Reduce size
        self.arr.pop()  # Remove last duplicate element

    def __str__(self):
        return ' -> '.join(str(self.arr[i]) for i in range(self.size)) + ' -> null'

# Driver code
array = Array([10, 20, 30, 40])
print("Before deletion:", array)
array.delete_at_beginning()
print("After deletion:", array)
if self.size == 0:
Check if array is empty to avoid errors
for i in range(1, self.size):
Loop through elements starting from second to shift left
self.arr[i - 1] = self.arr[i]
Move each element one position left
self.size -= 1
Decrease size to reflect deletion
self.arr.pop()
Remove last duplicate element after shifting
OutputSuccess
Before deletion: 10 -> 20 -> 30 -> 40 -> null After deletion: 20 -> 30 -> 40 -> null
Complexity Analysis
Time: O(n) because we shift all elements one position to the left
Space: O(1) because we modify the array in place without extra storage
vs Alternative: Compared to linked lists where deletion at beginning is O(1), array deletion requires shifting elements, making it slower
Edge Cases
empty array
No deletion occurs, array remains empty
DSA Python
if self.size == 0:
array with one element
After deletion, array becomes empty
DSA Python
self.size -= 1
When to Use This Pattern
When you need to remove the first item from a list and keep order, think of shifting elements left in an array.
Common Mistakes
Mistake: Not reducing the size after shifting, causing duplicate last element
Fix: Decrease size and remove last element after shifting
Mistake: Trying to delete from an empty array without check
Fix: Add a condition to check if array is empty before deletion
Summary
Deletes the first element of an array by shifting all other elements left.
Use when you need to remove the front item but keep the order of remaining items.
The key is shifting elements left and reducing the array size to avoid duplicates.