Group Values by Key

Create one list per sensor and append every matching reading without confusing grouping with counting or latest-value replacement.

A count answers how many readings each sensor produced. It does not retain the readings themselves. For a history such as this one, each sensor needs a list:

Do Not Overwrite a Repeated Key

Assigning each reading directly to its sensor keeps only the latest value:

The second north reading replaces the first north reading. That is correct for a “latest reading” dictionary, but not for a complete history.

For grouping, each dictionary value is a list. Create that list the first time a sensor appears, then append every reading for that sensor:

The first-use condition matters. Without it, the first lookup for a sensor would ask for a list that does not exist. With it, later occurrences reuse and change the existing inner list.

Q1. Create a group on first use

The dictionary is empty and the current pair is ("north", 18). Which two steps correctly store the first north reading?

Choose one

Select one choice, then check.

HintSeparate setup from collection

On first use, the sensor key has no associated list yet.

SolutionInitialize, then append

First assign an empty list to the "north" key. Appending 18 then changes that inner list to [18].

Not attempted
Review

Not marked done.

Trace the Outer Dictionary and Inner Lists

The dictionary and its lists have different jobs. The outer dictionary finds a group by sensor name. The inner list preserves that sensor's readings in source order.

Input pairOuter keys after the stepInner lists after the step
("north", 18)northnorth: [18]
("east", 21)north, eastnorth: [18], east: [21]
("north", 20)north, eastnorth: [18, 20], east: [21]
("west", 19)north, east, westnorth: [18, 20], east: [21], west: [19]
("east", 22)north, east, westnorth: [18, 20], east: [21, 22], west: [19]

On the third step, the outer keys do not change. Only the north inner list grows. On the fifth step, only the east inner list grows.

A count and a group answer different questions:

Use a count when only frequency matters. Use a group when later work needs the individual values.

Q2. Choose between counting and grouping

The next calculation needs the minimum reading from each sensor. Which result preserves the required evidence?

Choose one

Select one choice, then check.

HintPreserve the future input

Finding a minimum requires the actual readings, not only their number.

SolutionKeep a list for each sensor

The grouped dictionary preserves all readings by sensor. The count dictionary and latest-value dictionary cannot recover a discarded earlier reading.

Not attempted
Review

Not marked done.

Derive One Result from Each Complete Group

After grouping is complete, a second loop can derive one value per sensor. For example, build a dictionary of group sizes:

This loop visits unique dictionary keys, so each assignment creates one unambiguous result. The equivalent dictionary comprehension is:

Read it from the for clause: take each sensor and completed reading list, then associate the sensor with that list's length.

A comprehension is not a replacement for the grouping loop. This expression does not preserve repeated readings:

Each repeated key replaces its previous value, just as direct key assignment did earlier. Group with the visible first-use loop; use the comprehension only for a one-result-per-existing-key transformation after the groups exist.

Q3. Build complete sensor groups

Repair the loop so the alphabetical report shows every reading for each sensor.

Editable Python

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

Ready to run.

HintCreate each inner list once

Test membership before assigning []. Append outside that condition so both first and later readings enter the group. Sort the keys only when displaying.

SolutionGroup before reporting
Not attempted
Review

Not marked done.

A grouping dictionary maps each key to a list and grows that inner list on repeated input. A count keeps only frequency; a group keeps the values. Once the groups exist, a loop or a simple dictionary comprehension can derive one result per key. The next lesson uses a set when presence and uniqueness are the only questions.

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.