Count by Key

Turn repeated classification labels into one checked count per key, beginning with a visible first-use loop before using zero for an unseen label.

A measurement stream may contain the same classification many times:

classifications = ["ordinary", "high", "ordinary", "low", "ordinary", "high"]

The program does not need every repeated label in its final report. It needs one count for each distinct label.

Build the Count with an Explicit Loop

Begin with an empty dictionary. On the first occurrence of a label, add a zero count. Then update that count:

The dictionary grows as the loop visits each label:

Input labelDictionary after the update
"ordinary"{"ordinary": 1}
"high"{"ordinary": 1, "high": 1}
"ordinary"{"ordinary": 2, "high": 1}
"low"{"ordinary": 2, "high": 1, "low": 1}
"ordinary"{"ordinary": 3, "high": 1, "low": 1}
"high"{"ordinary": 3, "high": 2, "low": 1}

The repeated key does not create another entry. Assignment replaces its old count with the updated count.

Q1. Trace a repeated key

What is counts after this loop?

Choose one

Select one choice, then check.

HintUpdate rather than duplicate

After two iterations, the low count is 1 and the ordinary count is 1.

SolutionThe low count becomes two

The third label uses the existing "low" key and changes its value to 2, so the final dictionary is {"low": 2, "ordinary": 1}.

Not attempted
Review

Not marked done.

Use Zero as the Starting Count

For a count, zero has a precise meaning: the label has appeared zero times so far. That makes .get(label, 0) a valid optional lookup:

The first time "ordinary" appears, it is absent, so .get supplies 0 and the assignment stores 1. The next time, .get returns the existing count and the assignment adds one.

The shorter update expresses the same steps:

Use this form only after the longer trace is clear. The default zero is correct for a frequency count; it would not have been a valid replacement for an unknown sensor reading in Lesson 1.

Q2. Explain the first update

counts is empty and label is "high". What value does this expression assign to counts["high"]?

counts[label] = counts.get(label, 0) + 1

Compute it first, then check your number.

HintThe key has not appeared yet

.get("high", 0) returns the stated default.

SolutionStore the first occurrence

The lookup supplies 0, and 0 + 1 is 1. Assignment creates the "high" key with count 1.

Not attempted
Review

Not marked done.

Read Keys, Values, or Pairs

An ordinary dictionary loop visits its keys:

The .keys() method states the same choice explicitly:

Use .values() when only the counts matter. For example, their sum should match the number of input labels:

print(sum(counts.values()))  # 6

Use .items() when both parts matter. Each item is a key-value pair, which the loop can unpack into two names:

The dictionary preserves first-insertion order, but a report should state its ordering rule rather than depend on the input's first occurrence. Sort the keys for an alphabetical report:

This produces high, low, and ordinary in a predictable order.

Q3. Build and report classification counts

Complete the keyed update and sort the final report. The program should display high 2, low 1, and ordinary 3 in alphabetical order.

Editable Python

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

Ready to run.

HintComplete the update before the report

Assign counts.get(label, 0) + 1 to the current key, then wrap counts in sorted(...) in the report loop.

SolutionCount and display by key
Not attempted
Review

Not marked done.

A keyed count keeps one number for each repeated label. The visible first-use loop and .get(label, 0) express the same zero start. Keys, values, and items provide different views of that summary; the next lesson keeps the readings themselves instead of reducing each sensor to one number.

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.