Look Up Values by Key

Connect each sensor label to one value, then distinguish a required bracket lookup from membership or an optional result with a defined missing-value meaning.

Chapter 5 kept each sensor beside its reading in a tuple:

This ordered list is useful when the program visits every measurement. If the question is “What is the east sensor reading?”, searching by a numbered position is fragile. The sensor name is the meaningful identifier.

Store Each Value under a Key

A dictionary associates a key with a value. Curly braces enclose the entries, and a colon separates each key from its value:

Here the sensor names are keys and the readings are values. Square brackets look up a value by its exact key:

The lookup does not depend on "east" being the second entry. It asks for the value associated with that name.

Key used for lookupAssociated value
"north"18
"east"21
"west"20

Q1. Choose a meaningful lookup

The order of the dictionary may change, but the program must retrieve the west sensor reading. Which expression states that request?

Choose one

Select one choice, then check.

HintName the sensor

A dictionary lookup places the required key inside square brackets.

SolutionLook up the west key

readings_by_sensor["west"] returns the value associated with the west sensor, which is 20.

Not attempted
Review

Not marked done.

Check before a Required Lookup

Bracket lookup means that the key is required. If the dictionary has no "roof" key, this expression cannot return the requested reading:

readings_by_sensor["roof"]

Python reports a KeyError. When the key may be absent, membership makes the two paths explicit:

Use .get() only when its fallback has a defined meaning. In this measurement program, None already means that no numerical reading is available:

This shorter form is suitable only when a missing key and a stored None value may both mean “no reading available.” If the program must distinguish “unknown sensor” from “known sensor with an unavailable reading,” test membership first.

Q2. Choose a valid optional lookup

The program defines None to mean “no numerical reading is available.” Which expression may use that same result when the "roof" key is absent?

Choose one

Select one choice, then check.

HintPreserve the meaning of absence

Zero would be a real numerical reading in this program.

SolutionUse the established None result

readings_by_sensor.get("roof") returns None when the key is absent. That fallback is valid because the program already uses None for an unavailable numerical reading.

Not attempted
Review

Not marked done.

Add or Replace One Entry

Assignment through a key changes the dictionary. A new key adds an entry:

readings_by_sensor["roof"] = 17

An existing key replaces its associated value:

readings_by_sensor["east"] = 22

The visible changes are:

InstructionKeys afterwardEast value afterward
starting dictionarynorth, east, west21
add roofnorth, east, west, roof21
replace eastnorth, east, west, roof22

A dictionary keeps entries in insertion order. Replacing east does not move it, and adding roof places that key after the earlier keys. This is useful when reading a trace, but a lookup should still depend on the key. When a report needs a stated alphabetical order, request it explicitly:

An empty dictionary has length zero:

After that relation is clear, an empty dictionary may be used as false and a non-empty dictionary as true in a condition:

Q3. Update and report by key

Add the roof sensor, replace the east reading, and keep the final report alphabetical. The program should display the four lines shown by the checker.

Editable Python

Command/Ctrl + Enter. Python runs in your browser.

Ready to run.

HintMake all three choices visible

Use one assignment for "roof", one for "east", and wrap the dictionary in sorted(...) in the loop header.

SolutionChange by key and sort the report
Not attempted
Review

Not marked done.

A dictionary retrieves and updates values by meaningful keys. Brackets state that a key is required; membership or a defined .get() fallback handles absence. One value per key is enough for lookup, but repeated labels need an update pattern that records how often each one occurs.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 3 exercises marked done

Review

Not marked done.