All subjects · Computer Science · WCP02 Practical Programming and Problem-solving
Operations on data structures: create, insert, update, retrieve, delete, traverse
Edexcel IAL Computer Science · WCP02 Practical Programming and Problem-solving
Whatever the structure, you need the same six operations. Knowing the Python for each one, for lists and dictionaries, saves time in the exam.
The six operations (spec 8.2.4)
| Operation | List | Dictionary |
|---|---|---|
| Create | items = [] or list() | d = {} or dict() |
| Insert | items.append(x) (end); items.insert(i, x) (at index i) | d[key] = value (new key) |
| Update | items[i] = x | d[key] = value (existing key) |
| Retrieve | items[i]; items[a:b] | d[key]; d.get(key, default) |
| Delete | del items[i]; items.pop(i); items.pop() removes the last | del d[key] |
| Traverse | for x in items: or for i in range(len(items)): | for k, v in d.items(): |
Lists in action
queue = ["Ana", "Ben"]
queue.append("Cai")
queue.insert(0, "Dev")
queue[1] = "Ann"
removed = queue.pop()
del queue[0]
print(queue, removed)
print(queue + ["Eve"])Output:
['Ann', 'Ben'] Cai
['Ann', 'Ben', 'Eve']Deleting while traversing
Never delete items from a list while a for loop is going through it: the indexes shift and items are skipped. Build a new list of the items to keep instead.
nums = [4, 4, 7, 4]
for n in nums:
if n == 4:
nums.remove(n)
print(nums)
nums = [4, 4, 7, 4]
kept = []
for n in nums:
if n != 4:
kept.append(n)
print(kept)Output:
[7, 4]
[7]Copies and references
b = a does not copy a list: both names refer to the same list. Use copy.copy(a) (or a[:]) for a shallow copy, and copy.deepcopy(a) for a 2D list. The sample paper rewarded leaving the original data unchanged.
import copy
a = [[1, 2], [3, 4]]
b = a
c = copy.deepcopy(a)
a[0][0] = 99
print(b[0][0], c[0][0])Output:
99 1Worked example
Starting from tasks = ["wash", "cook"], insert "shop" at the front, change "cook" to "bake", then remove "wash".
- tasks.insert(0, "shop") gives ["shop", "wash", "cook"]
- tasks[2] = "bake"
- del tasks[1]
- Result: ['shop', 'bake']
Answer: ['shop', 'bake']
Check yourself
- What is displayed when this code runs?
```python
x = [5, 6, 7]
x.insert(1, 9)
x.append(1)
print(x)
```
- [5, 6, 9, 7, 1]
- [9, 5, 6, 7, 1]
- [5, 9, 6, 7, 1]
- [1, 5, 9, 6, 7]
Show answer
[5, 9, 6, 7, 1]. insert(1, 9) puts 9 at index 1 and append adds 1 at the end: [5, 9, 6, 7, 1].
- What is displayed when this code runs?
```python
stack = [3, 8, 2, 6]
a = stack.pop()
b = stack.pop(0)
print(a + b, len(stack))
```
Show answer
9 2. pop() removes the last item (6) and pop(0) the first (3): 9, leaving 2 items: 9 2.
- What is displayed when this code runs?
```python
marks = {"Ana": 7, "Ben": 4, "Cai": 9}
del marks["Ben"]
marks["Dee"] = 5
print(list(marks.keys()))
```
- ['Ana', 'Ben', 'Cai', 'Dee']
- ['Dee', 'Ana', 'Cai']
- ['Ana', 'Cai', 'Dee']
- ['Ana', 'Cai']
Show answer
['Ana', 'Cai', 'Dee']. Ben is removed and Dee is added at the end: ['Ana', 'Cai', 'Dee'].
- What is displayed when this code runs?
```python
a = [1, 2, 3]
b = a
b.append(4)
print(len(a))
```
Show answer
4. b = a makes both names refer to the same list, so a also has 4 items: 4.
- What is displayed when this code runs?
```python
import copy
a = [1, 2, 3]
b = copy.copy(a)
b.append(4)
print(len(a))
```
Show answer
3. copy.copy makes a new list, so a still has 3 items.
- What is displayed when this code runs?
```python
prices = {"tea": 2, "bun": 3, "jam": 4}
for item in prices:
prices[item] = prices[item] * 2
print(prices["bun"])
```
Show answer
6. Each value is updated during the traversal; bun becomes 6.
- This code should remove every negative number from readings, giving [4, 7, 2]. Which line completes it?
```python
readings = [4, -1, -3, 7, 2]
kept = []
for r in readings:
__________
kept.append(r)
readings = kept
print(readings)
```
- `if r < 0:`
- `if r > 0 and r < 7:`
- `if readings[r] >= 0:`
- `if r >= 0:`
Show answer
`if r >= 0:`. Keep the items that are not negative.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Computer Science specification (XCP01/YCP01), first teaching 2026
- Pearson IAL Computer Science Unit 2 Programming Language Subset (PLS), Python 3
- Pearson IAL Computer Science IAS sample assessment materials (Unit 2 paper and mark scheme)
Last updated 8 November 2026. Spotted a mistake? Tell us.