KS3 & GCSE Computing · GCSE

How to Iterate Over a Python Dictionary — GCSE Computer Science

Learn how to iterate over Python dictionaries for GCSE Computer Science: keys(), values(), items(), and writing loops that process dictionary data efficiently.

Duke Harewood — author of AI Tutors for Key Stage 3Updated 3 min read

On this page

Short answer

A Python dictionary maps unique keys to values — for example, student names to their scores. Iterating over a dictionary means visiting each entry in turn. Python provides three built-in methods — .keys(), .values(), and .items() — that control exactly what each loop iteration hands you, giving precise control over how you process dictionary data.

At a glance

Key stage
GCSE
Subject
Computing
Type
How-to guide
For
Students
Read time
3 min
Last updated
21 October 2026

Where this fits

  1. Key Stage 3Years 7–9
  2. GCSEYears 10–11This article
This article is aimed at GCSE (Years 10–11), the stage after Key Stage 3 (Years 7–9).

Method at a glance

  1. Use .keys() to loop through dictionary keys
  2. Use .values() to loop through dictionary values only
  3. Use .items() to loop through both keys and values together
The 3 numbered steps in this article, in order.

What happens when you loop over a dictionary directly?

If you write a for loop with just the dictionary name, Python iterates over the keys by default:

scores = {"Alice": 88, "Bob": 72, "Chloe": 95}

for student in scores:
    print(student)
# Output:
# Alice
# Bob
# Chloe

This is identical to writing for student in scores.keys(). The values are not directly accessible inside the loop — you must use scores[student] to look them up.

How do you iterate over keys, values, and key-value pairs?

Follow these steps to choose the correct iteration method for a given task:

  1. Use .keys() to loop through dictionary keys. This is the default, but writing .keys() makes your intent explicit and readable. Use it when you need only the key and will look up values manually.
  2. Use .values() to loop through dictionary values only. Use this when you need every value for a calculation but do not need the associated key — for example, summing all scores.
  3. Use .items() to loop through both keys and values together. Python unpacks each entry into two variables simultaneously. This is the most commonly used method for tasks that need both the key and the value in the same iteration.
scores = {"Alice": 88, "Bob": 72, "Chloe": 95}

# Step 1: iterate over keys
for name in scores.keys():
    print(name)

# Step 2: iterate over values
total = 0
for score in scores.values():
    total += score
print("Total:", total)          # Total: 255

# Step 3: iterate over key-value pairs
for name, score in scores.items():
    print(f"{name}: {score}")
# Output:
# Alice: 88
# Bob: 72
# Chloe: 95

How do you find the highest or lowest value in a dictionary?

A common GCSE task is to find the key with the maximum or minimum value. You can combine .items() with max() or use a loop:

scores = {"Alice": 88, "Bob": 72, "Chloe": 95}

# Using max() with a key function
top_student = max(scores, key=scores.get)
print(top_student)              # Chloe

# Using a loop (easier to understand at GCSE)
best_name = ""
best_score = -1
for name, score in scores.items():
    if score > best_score:
        best_score = score
        best_name = name
print(best_name, best_score)    # Chloe 95

How do you count occurrences with a dictionary?

Dictionaries are ideal for tallying frequencies — building a count of how many times each item appears in a list:

words = ["cat", "dog", "cat", "fish", "dog", "cat"]
counts = {}

for word in words:
    if word in counts:
        counts[word] += 1
    else:
        counts[word] = 1

for word, count in counts.items():
    print(f"{word}: {count}")
# cat: 3
# dog: 2
# fish: 1

This pattern — checking membership with in then updating or inserting — appears frequently in GCSE programming tasks.

What is a dictionary comprehension?

A dictionary comprehension creates a new dictionary in a single expression, similar to a list comprehension. The syntax is {key: value for item in iterable}:

scores = {"Alice": 88, "Bob": 72, "Chloe": 95}

# Create a new dict with grades A/B/C
grades = {
    name: "A" if score >= 90 else "B" if score >= 75 else "C"
    for name, score in scores.items()
}
print(grades)
# {'Alice': 'B', 'Bob': 'C', 'Chloe': 'A'}
Method When to use
for key in dict Need only keys
for val in dict.values() Need only values
for k, v in dict.items() Need both key and value
{k: v for ...} comprehension Build a new transformed dictionary

Frequently asked questions

In what order does Python iterate over a dictionary?

From Python 3.7 onwards, dictionaries maintain insertion order — meaning a for loop visits entries in the order they were added. Earlier versions of Python 3 (and Python 2) made no guarantee about order. At GCSE you can rely on insertion order when using Python 3, but it is good practice to mention this assumption explicitly.

Can you modify a dictionary while iterating over it?

No — modifying a dictionary's size (adding or deleting keys) during a for loop raises a RuntimeError: dictionary changed size during iteration. If you need to remove entries based on a condition, collect the keys to delete in a separate list first, then loop over that list to delete them after the main iteration.

What is the difference between dict.items() and dict.keys()?

.keys() returns a view of just the keys; .values() returns just the values; .items() returns each key-value pair as a tuple. The for name, score in scores.items() syntax uses tuple unpacking — Python automatically assigns the first element of each tuple to name and the second to score.

Why is iterating with .items() better than using square-bracket lookup?

Writing for name in scores: print(scores[name]) works but performs a dictionary lookup (scores[name]) on every iteration. Writing for name, score in scores.items() gives you both values directly without an extra lookup. For small dictionaries the difference is negligible, but .items() is considered cleaner, more Pythonic, and is what most GCSE mark schemes expect.


For Socratic GCSE tutoring and free Computer Science flashcards, see aitutors.me.

Key terms

  • dictionary
  • keys
  • values
  • dictionary comprehension
  • insertion order
  • tuple
  • tuple unpacking

Sources