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 n, an insertion position may be any index from 0 through n, inclusive. Index 0 inserts at the beginning. Index n 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≤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 n, a deletion index must identify an existing element: the indexes run from 0 through n−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<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
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
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.
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".
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
The former elements after index 0 shift left.
Python 3
items = [12, 25, 31, 44]
removed = items.pop(0)
Identify the element
Index 0 is valid and contains 12. This is the value that will be removed.
Close the gap
The former value at index 1, 25, takes index 0. Then 31 takes index 1, and 44 takes index 2.
Check the result
The list now contains the three values that were not deleted, in their original order.
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.