🐸

The Count Ledger

The Data Tinkererpython-data-tinkerer-13-the-count-ledger
Reward: 110 XP
|

The Count Ledger

Right after the shared-shelf comparison, another stack of borrowing slips lands on the record-room desk. Each slip shows one scroll title. Some titles appear once, while others show up again and again. The question is no longer “is this title here?” but “how many times did this title appear?”

At that point, it is not enough to keep piling titles into a plain list. Hoppy needs a tiny ledger: each scroll title should point to a number that remembers how many times it was requested. This is where a dict starts to feel like a real workbench for records.

A dict can organize “name -> count” records

You have already seen the basic shape of a dict before. This lesson does not re-teach curly braces from the beginning. Instead, it puts dictionaries into a more realistic job: when the same title can appear many times, we do not want a repeated pile anymore. We want to know how many times each title appeared.

borrow_log = ["Moon Map", "Fern Atlas", "Moon Map", "Bell Log"]
count_ledger = {}

print("Borrow log:", borrow_log)

for title in borrow_log:
  if title not in count_ledger:
      count_ledger[title] = 0

  count_ledger[title] += 1

print("Count ledger:", count_ledger)

In this code, the keys in the dict are scroll titles, and the values are the counts. The first time a title appears, you give it a starting value of 0. After that, every sighting adds 1 to that title.

Keys organize the record, values hold the quantity

The main instinct here is not “memorize more dictionary syntax.” It is to see the shape of the ledger clearly: a title like "Bell Map" becomes a key, and a number like 3 becomes its value. A messy pile of borrowing slips turns into a compact count record.

You do not need nested dictionaries here, and you do not need a complete dictionary tour either. Just keep one useful picture in mind: when you need to organize “name -> count,” a dictionary feels natural.

1
Look at the prepared borrowing log and the empty ledger

The starter already gives you borrow_log and an empty count_ledger. The repeated titles inside borrow_log are exactly what you will count.

2
Fill in the first-time setup and the +1 update inside the loop

The starter already gives you for title in borrow_log. Your job is to say: if the title is not in count_ledger yet, start it at 0; then add 1 for that title.

3
Run it and inspect the finished count ledger

When you run the code, you will see the original borrowing log and the finished Count ledger:. The key idea to keep is that a dict does not only store information. It can also fold repeated data into a clear statistic.

Why use a dict here?

Because this job is naturally “one name, one quantity.” If you keep only a list, you still have to count again and again. If you use titles as keys and counts as values, the record immediately becomes a readable ledger.

Suggested Solution
Expand
Solution:
borrow_log = ["Bell Map", "Moss Atlas", "Bell Map", "Star Ledger", "Bell Map", "Moss Atlas"]
count_ledger = {}

print("Borrow log:", borrow_log)

for title in borrow_log:
  if title not in count_ledger:
      count_ledger[title] = 0

  count_ledger[title] += 1

print("Count ledger:", count_ledger)
Advanced Tips
Want more? Click to expand

You do not need to think of dictionaries as a huge topic yet. Just keep this workbench feeling: when you hold a stream of repeated records, a dictionary can organize them into a result shaped like “title -> count.”

Later, when you meet tasks like “who came how many times,” “which tag appeared how many times,” or “which material was requested how many times,” you can remember this count ledger. For counting jobs like these, a dict is often more natural than a plain repeated list.

Loading...
Terminal
Terminal
Ready to run...