DoAssignment study guide

A3.3 · Insert and delete array elements

Learn to insert and delete array elements through clear examples and targeted practice.

Ontario Grade 12 Computer Science

Programming Concepts and Skills

ICS4U A3.3 study lesson

An array stores elements in an ordered set of positions identified by indexes. Inserting or deleting an element can change which value occupies a position. This lesson focuses on those operations: choosing a position, preserving the other values, and checking the resulting sequence. The examples use Python 3 lists as indexed sequences.

What you will learn

  • Explain how insertion and deletion change the positions of array elements.
  • Choose a valid index for an insertion or deletion.
  • Trace short Python 3 examples and state their expected results.

1. Review: values, indexes, and the goal

An indexed sequence has an order. In Python, the first position has index 0, the next has index 1, and so on. For example, in ["red", "blue", "green"], the value at index 1 is "blue". An index identifies a position; an element is the value stored there.
Insertion adds a value at a chosen position. The original values at that position and after it must move one position right so they are not overwritten. Deletion removes the value at a chosen position. The values after it move one position left to close the gap.
A useful trace shows the sequence before the change, marks the target index, and shows the sequence afterward. Check that every value that should remain is present and in the intended order.
  • Indexes identify positions, not stored values.
  • Insertion makes room by shifting later values right.
  • Deletion closes a gap by shifting later values left.

2. Insertion: make room, then place the value

For a sequence of length nn, an insertion position may be any index from 0 through nn, inclusive. Index 0 inserts at the beginning. Index nn inserts after the current last element.
When shifting values manually, start at the last occupied index and work backward toward the insertion index. Moving backward protects values that have not yet been copied: each value reaches its new position before an earlier value is moved. Then put the new value in the open position.
Python lists can grow, so the example makes one extra position with append before shifting. Preconditions: the requested index is from 0 through the current length. Inputs: the list, insertion index, and new value. The expected output is the list with the new value at that index and the old values in their original order.
0≤index≤n0 \leq index \leq n
  • Valid insertion indexes range from 0 through the current length, inclusive.
  • Shift from right to left to avoid overwriting an old value before it moves.
  • Insertion increases the list length by one.

3. Deletion: remove the value and close the gap

For a non-empty sequence of length nn, a deletion index must identify an existing element: the indexes run from 0 through n−1n-1. After the chosen value is removed, each following value takes the preceding position.
In Python, pop(index) removes and returns the value at that index. Its precondition is that the index identifies an existing element. Inputs: a list and a valid deletion index. Expected output: the shortened list, with the removed value available as the return value of pop.
When tracing, distinguish the removed value from the new value at the same index. If there was a following element, it takes the deleted element’s former position.
0≤index<n0 \leq index < n
  • Valid deletion indexes run from 0 through one less than the current length.
  • Elements after the deleted one take the preceding positions.
  • Deletion decreases the list length by one.

4. Trace and check the result

Record indexes as well as values. For insertion, mark the intended position and follow shifts from the end toward it. For deletion, mark the value to remove and track the later values as they move left. Then count the elements and verify their order.
Before changing the sequence, check the index against its length. An insertion index outside the allowed range or a deletion index that does not identify an element fails the stated preconditions.
The examples use short Python 3 code. Their outputs are derived by tracing the operations; the code is presented for reading, not as a report of execution.
  • Check the index against the current sequence length.
  • After insertion, expect one additional element; after deletion, expect one fewer.
  • Preserve the order of all elements that remain.

Worked example

Insert a value in the middle

Given ["oak", "elm", "ash"], insert "fir" at index 1. What is the resulting list?
List after insertion
Index → stored value0oak1fir2elm3ash

Indexes and values in the expected result.

Python 3

items = ["oak", "elm", "ash"]
index = 1
value = "fir"
items.append(None)
for i in range(len(items) - 1, index, -1):
    items[i] = items[i - 1]
items[index] = value
  1. Check the insertion position
    The list has three elements, so index 1 is a valid insertion position. The old values at indexes 1 and 2 must move right.
  2. Trace the shift
    The last value, "ash", moves from index 2 to index 3. Then "elm" moves from index 1 to index 2. Index 1 is open for "fir".
  3. State the result
    The original values remain in their original order, with "fir" inserted between "oak" and "elm".
Answer: ["oak", "fir", "elm", "ash"]
Check: The result has four elements, one more than the original list.

Worked example

Delete the first element

Given [12, 25, 31, 44], delete the element at index 0. State the removed value and the resulting list.
List after deletion
Index → stored value025131244

The former elements after index 0 shift left.

Python 3

items = [12, 25, 31, 44]
removed = items.pop(0)
  1. Identify the element
    Index 0 is valid and contains 12. This is the value that will be removed.
  2. Close the gap
    The former value at index 1, 25, takes index 0. Then 31 takes index 1, and 44 takes index 2.
  3. Check the result
    The list now contains the three values that were not deleted, in their original order.
Answer: Removed value: 12. Resulting list: [25, 31, 44].
Check: The result has three elements, one fewer than the original list.

Worked example

Insert at the end, then delete an element

Start with ["pen", "pad"]. Insert "clip" at index 2, then delete index 1. What are the list and removed value after both operations?
List after both operations
Index → stored value0pen1clip

Final indexes and values after insertion and deletion.

Python 3

items = ["pen", "pad"]
items.insert(2, "clip")
removed = items.pop(1)
  1. Insert at the end
    The starting list has length two, so index 2 is a valid insertion position. No existing values need to move; the list becomes ["pen", "pad", "clip"].
  2. Delete index 1
    Index 1 contains "pad", so that value is removed. The following value, "clip", shifts into index 1.
  3. Report both results
    After the two operations, the list contains "pen" and "clip". The removed value was "pad".
Answer: Removed value: "pad". Final list: ["pen", "clip"].
Check: The insertion adds one element and the deletion removes one, so the final length is two.

Common mistakes and how to avoid them

Shifting values left to right during insertion.
Correction: Shift from the last occupied position backward. Otherwise, a value can be overwritten before it has been moved.
Treating the current length as a valid deletion index.
Correction: The last existing index is one less than the length. The length itself is the valid insertion position at the end.
Assuming deletion leaves later elements at their old indexes.
Correction: Later values move one position left so the resulting sequence has no gap.

Lesson summary

  • Insertion adds a value at a valid position and preserves existing values by shifting later values right.
  • Deletion removes a value at an existing index and closes the gap by shifting later values left.
  • Trace indexes and values, then check the order and the new length.

Check your understanding

Question 1

A list has four elements. Which index inserts a new value immediately after the current last element?
  1. Index 3
  2. Index 4
  3. Index 5
  4. Index 0
Show answer and explanation
Index 4
Indexes 0 through 3 identify the four existing elements. Index 4 is the valid insertion position after them.

Question 2

What is the result of deleting index 1 from ["sun", "moon", "star"]?
  1. ["sun", "star"]
  2. ["moon", "star"]
  3. ["sun", "moon"]
  4. ["sun", "moon", "star"]
Show answer and explanation
["sun", "star"]
Index 1 contains "moon". The remaining value "star" shifts left, giving ["sun", "star"].

Question 3

When manually making room to insert into a list, in which direction should values be shifted?
  1. From the last occupied position toward the insertion position
  2. From the insertion position toward the last occupied position
  3. In any direction
  4. Do not shift any values
Show answer and explanation
From the last occupied position toward the insertion position
Shifting from right to left protects values that have not yet been moved from being overwritten.

Key terms

Array element
A value stored in an indexed position in an ordered sequence.
Index
A number that identifies a position in an indexed sequence.
Insertion
Adding an element at a chosen position while preserving the other elements.
Deletion
Removing an element at a chosen position while bringing later elements forward.

Continue through ICS4U

View the complete ICS4U Ontario Grade 12 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 12 Computer Science (ICS4U), expectation A3.3. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question