🐸

The Shape Choice

The Data Tinkererpython-data-tinkerer-14-the-shape-choice
Reward: 110 XP
|

The Shape Choice

As Chapter 2 nears its end, Hoppy steps up to a new sorting desk in the record room. There is no harder monster here, just four tiny task cards: one wants to keep visitor order, one wants to store a location as a fixed pair, one wants to keep only distinct seals, and one wants to turn repeated borrowing slips into counts.

This lesson is not about introducing another container. It is a light checkpoint: when a data job changes, can you start to feel why list, tuple, set, and dict belong in different places?

The four stations do not want to preserve the same thing

The key idea here is not memorizing how each shape looks. It is asking a more useful question: what does this job most want to preserve? Order? A fixed pair? Unique members only? Or a record shaped like “name -> count”?

first_visitor = "Nia"
second_visitor = "Sol"
third_visitor = "Tavi"

shelf_zone = "Amber Wing"
shelf_slot = 5

seal_log = ["sun", "fern", "sun", "fern"]
borrow_log = ["Moon Map", "Moon Map", "Bell Log"]

arrival_line = [first_visitor, second_visitor, third_visitor]
locator_card = (shelf_zone, shelf_slot)
unique_seals = set(seal_log)

borrow_counts = {}
for title in borrow_log:
  if title not in borrow_counts:
      borrow_counts[title] = 0

  borrow_counts[title] += 1

print("Arrival line:", arrival_line)
print("Locator card:", locator_card)
print("Unique seals:", unique_seals)
print("Borrow counts:", borrow_counts)

This code is really making four small choices. A visitor line should keep order, so it becomes a list. A location card only has one fixed zone-and-slot pair, so it becomes a tuple. The seal log has repeats, but we only care about the different seals, so it becomes a set. The borrowing slips repeat titles, and we want each title to point to a count, so that becomes a dict.

Look at the job first, then choose the shape

You do not need to re-teach all four structures from scratch here, and you do not need a heavy project either. Just keep one useful line in your head: need order -> think list, need a fixed pair -> think tuple, need uniqueness -> think set, need name-to-result records -> think dict.

That is the real goal of this chapter checkpoint: when you face data, stop naming containers like flash cards and start asking which shape the job actually needs.

1
Match the first three task cards to the right structures

The starter already gives you three visitors, one shelf location, and a repeated seal log. Fill in arrival_line, locator_card, and unique_seals so they become a list, a tuple, and a set.

2
Turn the last task card into a dict ledger

borrow_log repeats scroll titles. Just like the previous lesson, use the prepared for title in borrow_log loop to handle the first time a title appears, then add 1 so the result lands in borrow_counts.

3
Run it and inspect the four output lines

When you run the code, you will see all four structures side by side: an ordered arrival line, one fixed locator card, a de-duplicated seal set, and a title-to-count ledger. Look at each line and ask again: why does this shape fit this job?

One very useful question

When you receive some data, do not rush to recite definitions. Ask what you most need to preserve: order, a fixed combination, unique members, or key-value relations. Very often, the structure choice becomes much clearer right away.

Suggested Solution
Expand
Solution:
first_visitor = "Mira"
second_visitor = "Jun"
third_visitor = "Pip"

shelf_zone = "Fern Wing"
shelf_slot = 3

seal_log = ["sun", "moon", "sun", "leaf", "moon"]
borrow_log = ["Bell Map", "Moss Atlas", "Bell Map", "Star Ledger", "Bell Map", "Moss Atlas"]

arrival_line = [first_visitor, second_visitor, third_visitor]
locator_card = (shelf_zone, shelf_slot)
unique_seals = set(seal_log)

borrow_counts = {}
for title in borrow_log:
  if title not in borrow_counts:
      borrow_counts[title] = 0

  borrow_counts[title] += 1

print("Arrival line:", arrival_line)
print("Locator card:", locator_card)
print("Unique seals:", unique_seals)
print("Borrow counts:", borrow_counts)
Advanced Tips
Want more? Click to expand

If you look back across this chapter, you have now seen fixed pairs, unpacking, unique members, membership checks, set relationships, and count ledgers. This checkpoint is not asking you to become a theory expert. It is asking for one more practical reflex: different jobs call for different shapes.

That reflex will matter in the next chapter too. Soon you will start thinking about what shape a function should return. For today, the simpler win is enough: when you see a small job, you can choose a better container for it.

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