0Pricing
Coding Interview Prep · Lesson

Dictionaries as Lookup Tables

Map keys to values for fast access.

Dictionaries as Lookup Tables is a free Coding Interview Prep lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Coding Interview Prep learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Dictionary Idea

A dictionary maps keys to values, letting you fetch a stored answer by name instead of scanning. It is your contest lookup table. 🔑

Constant-Time Access

Like a set, a dict hashes the key, so reading and writing are roughly O(1). That speed is why it replaces slow linear searches.

Create a Dictionary

Write key and value pairs inside braces, or start empty with a plain pair of braces. Each key points to exactly one value.

age = {'amy': 21, 'bob': 19}
empty = {}

Read by Key

Fetch a value using square brackets around the key. The key acts like an index, but it can be a string, number, or tuple.

print(age['amy'])

Missing Keys Crash

Reading an absent key with brackets throws a KeyError. In a contest that turns into a runtime error verdict instantly.

Safe Reads with get

Use get to return a default instead of crashing when a key is absent. It is the safe way to read maybe-missing keys.

count = age.get('zoe', 0)

Insert and Update

Assigning to a key either adds it or overwrites the old value. The same syntax handles both insert and update.

age['cara'] = 25
age['amy'] = 22

Check Before You Read

Test presence with in before relying on a key. This guards against KeyError and keeps your logic explicit.

if 'bob' in age:
    print(age['bob'])

Map Values to Positions

A classic trick stores each value's index so you can later look up where it was in one step. Great for pair-finding tasks.

pos = {}
for i, v in enumerate(nums):
    pos[v] = i

Loop Over Pairs

Iterate keys and values together with items. It is cleaner than looking up each value again inside the loop.

for name, a in age.items():
    print(name, a)

Keys Must Be Hashable

Dictionary keys need to be hashable, so use numbers, strings, or tuples. A list cannot be a key, but a tuple of ints can.

seen = {(0, 0): 'start'}

Quick Check

You want to read a key that might not exist without crashing.

Recap

A dictionary maps keys to values for O(1) lookups. Use get for safe reads and store indices to turn slow scans into instant answers. 🚀

Frequently asked questions

Is the “Dictionaries as Lookup Tables” lesson free?

Yes — the full text of “Dictionaries as Lookup Tables” is free to read here on the web, and the Coding Interview Prep course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Coding Interview Prep course, upgrade to CoddyKit PRO.

What will I learn in “Dictionaries as Lookup Tables”?

Map keys to values for fast access. You practise Coding Interview Prep with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Coding Interview Prep?

No prior experience is required. Coding Interview Prep on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Dictionaries as Lookup Tables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Coding Interview Prep lesson?

Yes. Every Coding Interview Prep lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Sets for Membership and Dedup
  2. Dictionaries as Lookup Tables
  3. Counter and defaultdict in Action
  4. Group and Bucket with a Map
← Back to Coding Interview Prep