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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

The six operations (spec 8.2.4)

OperationListDictionary
Createitems = [] or list()d = {} or dict()
Insertitems.append(x) (end); items.insert(i, x) (at index i)d[key] = value (new key)
Updateitems[i] = xd[key] = value (existing key)
Retrieveitems[i]; items[a:b]d[key]; d.get(key, default)
Deletedel items[i]; items.pop(i); items.pop() removes the lastdel d[key]
Traversefor 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 1

Worked example

Starting from tasks = ["wash", "cook"], insert "shop" at the front, change "cook" to "bake", then remove "wash".

  1. tasks.insert(0, "shop") gives ["shop", "wash", "cook"]
  2. tasks[2] = "bake"
  3. del tasks[1]
  4. Result: ['shop', 'bake']

Answer: ['shop', 'bake']

Key idea: append/insert add, [i] = updates, [i] and get() retrieve, del and pop() remove, for loops traverse; do not delete during a for loop; b = a shares a list, deepcopy copies it.

Check yourself

  1. 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].

  2. 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.

  3. 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'].

  4. 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.

  5. 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.

  6. 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.

  7. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.