Dictionaries and Maps: Look Up by Key

Find a mark without guessing a list position

A teacher has student ID 009 and needs its maths mark. A sorted class list may move the student to another position, but the identifier should still locate the record.

A dictionary or map associates keys with values. A key identifies an entry independently of its current position. JavaScript Map is a built-in keyed collection; Python’s corresponding built-in is dict.

  1. Look up a key

    Retrieve a mark by identifier.

  2. Distinguish missing entries

    Use membership to separate an absent key from an empty stored value.

  3. Update deliberately

    Replace an existing value rather than accidentally creating another representation of the key.

Stable keys select values

  1. Key 007

    Lookup selects Maya’s maths mark.

    40
  2. Key 009

    Lookup selects Ravi’s maths mark.

    28
  3. Update 009

    Set a revised value at the same key.

    30
Read these stages alongside the complete example. They explain the relationship; they do not execute code.

A key must be interpreted consistently

A map can use keys of different types. For this school task, strings preserve formatted IDs such as “007.” A lookup uses the key’s equality rules, not how its display text looks.

get retrieves, set stores or replaces, has tests presence, and delete removes. Setting a key that already exists changes its value instead of appending a second copy of that key.

Before starting, review records and collections. These examples use JavaScript; other languages can use different syntax and rules.

Read and correct the same identifier

The first get reads 28. set replaces that value with 30, and the second lookup confirms the correction.

ID 011 is absent, so has is false and get returns undefined. The caller must decide what useful message an absent lookup needs.

JavaScript · local teaching example

const mathsById = new Map([['007', 40], ['009', 28]]);
console.log(mathsById.get('009'));
mathsById.set('009', 30);
console.log(mathsById.get('009'));
console.log(mathsById.has('011'));
console.log(mathsById.get('011'));

Expected output

28
30
false
undefined

Presence differs from the stored value

A stored undefined value and an absent key both produce undefined when read with get. has distinguishes them.

Use a deliberate missing-result convention such as null when a student exists but their mark has not been entered. Do not assume a falsy stored mark means an absent record.

JavaScript · local teaching example

const marks = new Map([['007', undefined], ['009', 0]]);
console.log(marks.has('007'));
console.log(marks.has('011'));
console.log(marks.get('009'));

Expected output

true
false
0

Apply the rule to a small change

Store 0 for identifier 011, then display whether the key exists and its mark.

Worked answer

The key exists and its value is zero. A membership check preserves that known mark instead of confusing it with missing data.

JavaScript · local teaching example

const marks = new Map();
marks.set('011', 0);
console.log(marks.has('011'));
console.log(marks.get('011'));

Expected output

true
0

Recognize mistakes and limits

An ordinary JavaScript object can also represent named fields, but its keys and prototype behavior differ from Map. Choose one intentionally; a student record and a table keyed by student ID have different roles.

Numeric 7 and string “007” are different keys. Separate object keys compare by identity. Define normalization and missing-key handling before building a lookup. A local map also provides no persistence by itself.

For the language or tool rules used here, see JavaScript sets and maps.

Check the core idea

JavaScript · code for this question

const mathsById = new Map([['007', 40], ['009', 28]]);
console.log(mathsById.get('009'));
mathsById.set('009', 30);
console.log(mathsById.get('009'));
console.log(mathsById.has('011'));
console.log(mathsById.get('011'));
What does set do when ID 009 already exists?
Choose one answer. You can try again.

Answer and explanation

Replaces the value for that key.

Yes. The corrected lookup then reads 30.

Apply the idea to a new situation

JavaScript · code for this question

const marks = new Map([['007', undefined], ['009', 0]]);
console.log(marks.has('007'));
console.log(marks.has('011'));
console.log(marks.get('009'));
Why use has when undefined might be stored?
Choose one answer. You can try again.

Answer and explanation

To distinguish a present entry from an absent key.

Yes. get alone returns undefined in both cases.

Look up the identity, then interpret the result

Keyed collections support stable lookup and replacement. Normalize keys, separate presence from stored value, and specify how missing records should be reported.

Connect this idea with membership without mapped values and comparing structures.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Stacks: Last In, First Out and Undo →