Wave 3 arrived: 300 respondents, but your pre-registered eligibility cutoff is age 18. In SPSS, how do you keep only the eligible rows?
I'd use is_eligible from Day 6 on a single respondent. But for 300, I'd set a filter variable in SPSS and rerun every table. Changing the cutoff means resetting the filter and rerunning again.
In Python, you iterate the list and check each respondent. append grows the result list one respondent at a time. The elegance is that is_eligible from Day 6 is the exact check you reuse here — no new logic, just applied to a collection:
respondents = [{"id": "R_001", "age": 22.0}, {"id": "R_002", "age": 15.0}]
eligible = []
for r in respondents:
if r["age"] >= 18.0:
eligible.append(r)What's r["age"]? Is that accessing the dict from the respondent list?
Exactly. Each r in the loop is one respondent dict. r["age"] looks up the "age" key in that dict — same as a codebook lookup. You're iterating the respondents list and filtering by a field inside each dict.
So I can change the cutoff to 21 and rerun — no filter variable, no re-clicking. The function handles it.
The entire SPSS eligibility filter, in four lines:
def filter_eligible(respondents: list, min_age: float) -> list:
eligible = []
for r in respondents:
if r["age"] >= min_age:
eligible.append(r)
print(f"Eligible: {len(eligible)} of {len(respondents)}")
return eligibleI ran the capstone scenario in my head — wave 4 arrives, I call filter_eligible, get 247 eligible, pass them to the grouping function. It's a pipeline.
One check: if a respondent dict is missing the "age" key, r["age"] raises KeyError. Use r.get("age", 0) for messy datasets where some rows lack the field entirely. Clean data first, filter second.
A list is an ordered collection. Build a filtered subset with a loop and append:
result = []
for item in collection:
if condition(item):
result.append(item)| Operation | Example | Meaning |
|---|---|---|
len(lst) | len(respondents) | number of items |
lst[0] | respondents[0] | first item |
lst[-1] | respondents[-1] | last item |
x in lst | r in eligible | membership test |
lst.append(x) | eligible.append(r) | add to end |
r["age"] raises KeyError if the key is absent. r.get("age", 0) returns 0 instead — safer for messy wave exports.
Wave 3 arrived: 300 respondents, but your pre-registered eligibility cutoff is age 18. In SPSS, how do you keep only the eligible rows?
I'd use is_eligible from Day 6 on a single respondent. But for 300, I'd set a filter variable in SPSS and rerun every table. Changing the cutoff means resetting the filter and rerunning again.
In Python, you iterate the list and check each respondent. append grows the result list one respondent at a time. The elegance is that is_eligible from Day 6 is the exact check you reuse here — no new logic, just applied to a collection:
respondents = [{"id": "R_001", "age": 22.0}, {"id": "R_002", "age": 15.0}]
eligible = []
for r in respondents:
if r["age"] >= 18.0:
eligible.append(r)What's r["age"]? Is that accessing the dict from the respondent list?
Exactly. Each r in the loop is one respondent dict. r["age"] looks up the "age" key in that dict — same as a codebook lookup. You're iterating the respondents list and filtering by a field inside each dict.
So I can change the cutoff to 21 and rerun — no filter variable, no re-clicking. The function handles it.
The entire SPSS eligibility filter, in four lines:
def filter_eligible(respondents: list, min_age: float) -> list:
eligible = []
for r in respondents:
if r["age"] >= min_age:
eligible.append(r)
print(f"Eligible: {len(eligible)} of {len(respondents)}")
return eligibleI ran the capstone scenario in my head — wave 4 arrives, I call filter_eligible, get 247 eligible, pass them to the grouping function. It's a pipeline.
One check: if a respondent dict is missing the "age" key, r["age"] raises KeyError. Use r.get("age", 0) for messy datasets where some rows lack the field entirely. Clean data first, filter second.
A list is an ordered collection. Build a filtered subset with a loop and append:
result = []
for item in collection:
if condition(item):
result.append(item)| Operation | Example | Meaning |
|---|---|---|
len(lst) | len(respondents) | number of items |
lst[0] | respondents[0] | first item |
lst[-1] | respondents[-1] | last item |
x in lst | r in eligible | membership test |
lst.append(x) | eligible.append(r) | add to end |
r["age"] raises KeyError if the key is absent. r.get("age", 0) returns 0 instead — safer for messy wave exports.
Theo has a wave-3 dataset of 300 respondent dicts, each with an `"age"` field. The pre-registration specifies a minimum age of 18 for inclusion. Write `filter_eligible(respondents, min_age)` that returns a new list containing only the dicts where age meets the minimum.
Tap each step for scaffolded hints.
No blank-editor panic.