← Back to course
Code Crew G5 / Week 06 / Thursday
4/6
Week 06 Β· Search one by one

Thursday

Search the star records
// Linear search and counting checks
⏱ about 15 min

Thursday: Search the star records

The catalog is more than names now. Each star has a record with its name and its glow.

Rocket wants to know how brightly Halo glows. "My find function only works on a list of names," he sighs.

Raven points at one record. "What do you notice about each item in the list?"

"Each one is a dictionary," Rocket says. "So I check its name key."

Nova blinks. "Here is a hint. The search is the same. Only the thing you compare changes."

A list of records

Each star is a dictionary. The loop looks at one dictionary at a time and checks its "name" key.

catalog = [
    {"name": "Ember", "glow": 4},
    {"name": "Frost", "glow": 9},
    {"name": "Glow", "glow": 2},
    {"name": "Halo", "glow": 7},
]

def glow_of(catalog, name):
    for star in catalog:
        if star["name"] == name:
            return star["glow"]
    return None

print(glow_of(catalog, "Halo"))
print(glow_of(catalog, "Pip"))
Output:
7
None
Using the same catalog, what does glow_of(catalog, "Ember") return?
WHY THIS EXERCISEThe search matches on the name, then returns a different key from the same record.

Search for the first match

A search does not have to look for an exact name. It can look for the first star that passes a test.

catalog = [
    {"name": "Ember", "glow": 4},
    {"name": "Frost", "glow": 9},
    {"name": "Glow", "glow": 2},
    {"name": "Halo", "glow": 7},
]

def first_bright(catalog, limit):
    for star in catalog:
        if star["glow"] > limit:
            return star["name"]
    return None

print(first_bright(catalog, 5))
Output:
Frost
USE FIRST_BRIGHT WITH THE SAME CATALOG.
  • Read the question.
  • Tap your answer.
What does first_bright(catalog, 3) return?
What does first_bright(catalog, 8) return?
What does first_bright(catalog, 9) return?

Practice lab: moon records

moons = [
    {"name": "Ash", "size": 3},
    {"name": "Bay", "size": 8},
    {"name": "Cove", "size": 5},
]

def size_of(moons, name):
    for moon in moons:
        if moon["name"] == name:
            return moon["size"]
    return None

def first_small(moons, limit):
    for moon in moons:
        if moon["size"] < limit:
            return moon["name"]
    return None

print(size_of(moons, "Cove"))
print(first_small(moons, 4))
What does size_of(moons, "Bay") return?
What does size_of(moons, "Dune") return?
What does first_small(moons, 6) return?
What does first_small(moons, 3) return?
RECORD CHECK
  • Read the question.
  • Tap your answer.
Which key does size_of compare with the target?
What does first_small(moons, 10) return?
What does first_small(moons, 6) skip over?
StatementTrue or false?
Each item in moons is a dictionary.?
size_of returns a name.?
first_small returns the first moon that passes the test.?
The program prints 5, then Ash.?
WHY THIS EXERCISEA search can match one key and return another.
What is size_of(moons, "Ash") + size_of(moons, "Bay")?
WHY THIS EXERCISEEach call returns a size, and the sizes add together.

Great work! Searching records is how real catalogs answer questions. Tomorrow we hunt a classic search bug.

← Wednesday