Array Deletion at Beginning in DSA Python - Time & Space Complexity
When we delete an element from the start of an array, the time it takes depends on how many elements need to move.
We want to know how the work grows as the array gets bigger.
Analyze the time complexity of the following code snippet.
def delete_first_element(arr):
if len(arr) == 0:
return arr
for i in range(1, len(arr)):
arr[i - 1] = arr[i]
arr.pop()
return arr
This code removes the first element of the array by shifting all other elements one step left.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that shifts elements left by one position.
- How many times: It runs once for each element after the first, so roughly n-1 times for an array of size n.
As the array size grows, the number of shifts grows almost the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 9 shifts |
| 100 | 99 shifts |
| 1000 | 999 shifts |
Pattern observation: The work grows linearly with the number of elements.
Time Complexity: O(n)
This means the time to delete the first element grows directly with the array size.
[X] Wrong: "Deleting the first element is always fast because it's just one removal."
[OK] Correct: Because all other elements must move to fill the gap, which takes time proportional to the array size.
Understanding this helps you explain why some data structures are better for front deletions, showing your grasp of efficiency.
"What if we deleted the last element instead of the first? How would the time complexity change?"
Practice
del arr[0]?Solution
Step 1: Understand
This command removes the element at index 0, which is the first element of the array.del arr[0]operationStep 2: Effect on the array
After deletion, all elements after the first shift one position to the left to fill the gap.Final Answer:
The first element is removed and the rest shift left by one position. -> Option AQuick Check:
Deleting first element shifts others left [OK]
- Thinking last element is removed
- Assuming array becomes empty
- Believing array stays the same
arr in Python?Solution
Step 1: Check Python syntax for deleting by index
Python usesdel arr[index]to delete element at a specific index.Step 2: Analyze options
del arr[0] uses correct syntaxdel arr[0]. del arr(0) uses parentheses which is invalid. arr.delete(0) uses a method that does not exist. arr.remove(0) removes by value, not index.Final Answer:
del arr[0] -> Option BQuick Check:
Use del with square brackets for index deletion [OK]
- Using parentheses instead of square brackets
- Confusing remove() with delete by index
- Trying to call a non-existent delete() method
arr = [10, 20, 30, 40] del arr[0] print(arr)
Solution
Step 1: Delete first element from array
The commanddel arr[0]removes the element 10 at index 0.Step 2: Remaining elements shift left
After deletion, the array becomes [20, 30, 40].Final Answer:
[20, 30, 40] -> Option AQuick Check:
First element removed, rest shifted left [OK]
- Expecting first element to remain
- Confusing deletion with removing last element
- Printing original array without deletion
arr:arr = [5, 10, 15] arr.delete(0) print(arr)
Solution
Step 1: Check list methods for deletion
Python lists do not have a method calleddelete(). Deletion by index usesdelkeyword orpop()method.Step 2: Identify correct deletion method
To delete first element, usedel arr[0]orarr.pop(0). Usingarr.delete(0)causes AttributeError.Final Answer:
The method delete() does not exist for lists. -> Option DQuick Check:
Use del or pop, not delete() [OK]
- Using delete() method on list
- Assuming delete() works like in other languages
- Ignoring AttributeError from wrong method
arr = [1, 2, 3, 4, 5]. You want to delete the first element and then add 0 at the beginning. Which code correctly does this in Python?Solution
Step 1: Delete first element correctly
Usingdel arr[0]removes the first element (1) from the array.Step 2: Insert 0 at the beginning
Usingarr.insert(0, 0)adds 0 at index 0, shifting others right.Step 3: Check other options
arr.pop(0) arr.append(0) print(arr) appends 0 at the end, not beginning. arr.remove(0) arr.insert(0, 0) print(arr) tries to remove value 0 which is not in array. del arr[1] arr.insert(0, 0) print(arr) deletes second element, not first.Final Answer:
del arr[0] arr.insert(0, 0) print(arr) -> Option CQuick Check:
Delete first, then insert at start [OK]
- Appending instead of inserting at start
- Removing value 0 instead of index 0
- Deleting wrong index
Ready to prove it?
Pass the test and this pattern moves unknown
