Programming, problem solving, and algorithms

CPSC 203, 2025 W2

February 12, 2026

Announcements

  • Examlet 2 is happening now good luck!
  • Project 1 due March 1

Dictionaries

Dictionaries are Python’s key-value lookup structure.

Beware: KeyError!

Why Dictionaries?

Lists: Access by position (index)

fruit = [("apple","red"), ("banana","yellow"), ("cherry","red")]
fruit[0], fruit[1], fruit[2]  # How do you look for "apple"?

Dictionaries: Access by meaningful key

fruit = {"apple":"red", "banana":"yellow", "cherry":"red"}
fruit["apple"], fruit["banana"], fruit["cherry"]  # Self-documenting!

Dictionaries are also fast — requires the same time whether there are 100 entries, or 100,000.

Dictionary Patterns

Pattern 0: Records

Alternative to a data frame or named tuple:

Pattern 1: Membership

Find everyone who was in Gallery A and Gallery B.

The Naive Approach: Check Every Pair

This works… but what if the lists are LONG?

The Problem with Nested Loops

With 10 people in Gallery A and 12 in Gallery B: 10 × 12 = 120 comparisons

With 10,000 in each: 100,000,000 comparisons!

Pattern 1: Membership with a Dictionary

Why Is This Faster?

  • Step 1: Look at each Gallery A person once → 10 operations
  • Step 2: Look at each Gallery B person once → 12 operations
  • Total: 10 + 12 = 22 operations (not 120!)

Dictionary lookup is instant — it doesn’t matter how many keys are stored!

Pattern 2: Counting Frequencies

Use case: Count how many times each item appears.

Pattern 3: Finding Complements

Use case: Find two numbers that add up to a target.

Do you like this?

Pattern 3: Finding Complements, reprise

Use case: Find two numbers that add up to a target.

As we scan, we remember what we’ve seen — and check if the complement exists!

Pattern 4: Grouping by Category

Use case: Organize items into categories

Grouping with Dictionaries

What if our items are dictionaries, not tuples?

Same pattern — just access the key with suspect["alibi"] instead of unpacking a tuple!

Pattern 5: Translation/Mapping

Use case: Convert values from one representation to another.

Beyond Caesar: Random Substitution

A Caesar cipher shifts by a fixed amount — we can decode with math:

plain_letter = chr(ord('A') + (i - shift) % 26)

But what if the spy used a random substitution? No formula can help!

Decoding with the Mapping

The dictionary is the cipher — no formula, just lookup!

Let’s Practice!

Open today’s activity. You’ll solve 2 new puzzles.

PrairieLearn Activity

Summary

Dictionaries give you fast lookup by key — whether you have 100 items or 100,000.

Pattern Use Case Technique
Record Store named fields {"name": ..., "age": ...}
Membership Track what we’ve seen seen[x] = True
Counting Count occurrences Counter(items)
Complement Find pairs Store & check complements
Grouping Organize by category defaultdict(list)
Translation Convert values Direct key → value mapping

Next Week

Reading Week — No Classes!

Enjoy the break, and keep working on Project 1.

See you in Week 8!