Your quality-control protocol flags the first respondent in each wave whose outcome score exceeds 1.5 standard deviations above the group mean. In SPSS, how do you find that respondent?
compute_outcome_per_respondent from Day 11 gives me the outcome list. In SPSS I'd sort descending and look at the first row. But that sorts the whole dataset — I just want the first outlier.
A while loop with break. You walk the list one item at a time and stop the moment you find what you need. No sorting, no full scan:
respondents = [{"id": "R_001", "outcome": 3.2}, {"id": "R_002", "outcome": 6.1}]
cutoff = 5.0
idx = 0
while idx < len(respondents):
if respondents[idx]["outcome"] > cutoff:
break
idx += 1What happens if no respondent exceeds the cutoff? Does the loop crash?
No crash — the condition idx < len(respondents) eventually becomes False and the loop exits normally. After the loop, you check whether idx reached the end. If it did, no outlier was found. Return an empty dict {} as a clean signal:
def find_first_outlier(respondents: list, cutoff: float) -> dict:
idx = 0
while idx < len(respondents):
if respondents[idx]["outcome"] > cutoff:
break
idx += 1
result = respondents[idx] if idx < len(respondents) else {}
print(f"First outlier: {result}")
return resultSo if the while loop exhausts the list, idx equals len(respondents) and I know no outlier exists. That's a clean exit condition.
The loop that knows when to give up. Every data quality check needs that property.
In SPSS I'd sort and scroll. Here I stop the moment I find it and keep the dataset order intact.
Order matters for time-series waves. Sorting scrambles entry order — which may be date of completion, a methodologically meaningful sequence. A while loop with break preserves it while still finding the target.
A while loop runs as long as its condition is True. break exits immediately:
idx = 0
while idx < len(items):
if condition(items[idx]):
break # stop here
idx += 1
# after loop: idx is either the found position or len(items)result = items[idx] if idx < len(items) else {}If idx == len(items), the loop ran out — nothing matched. Return a sentinel like {} to signal "not found" without raising an error.
for loops always complete. while + break stops early — essential when scanning large datasets where the first match is all you need.
Your quality-control protocol flags the first respondent in each wave whose outcome score exceeds 1.5 standard deviations above the group mean. In SPSS, how do you find that respondent?
compute_outcome_per_respondent from Day 11 gives me the outcome list. In SPSS I'd sort descending and look at the first row. But that sorts the whole dataset — I just want the first outlier.
A while loop with break. You walk the list one item at a time and stop the moment you find what you need. No sorting, no full scan:
respondents = [{"id": "R_001", "outcome": 3.2}, {"id": "R_002", "outcome": 6.1}]
cutoff = 5.0
idx = 0
while idx < len(respondents):
if respondents[idx]["outcome"] > cutoff:
break
idx += 1What happens if no respondent exceeds the cutoff? Does the loop crash?
No crash — the condition idx < len(respondents) eventually becomes False and the loop exits normally. After the loop, you check whether idx reached the end. If it did, no outlier was found. Return an empty dict {} as a clean signal:
def find_first_outlier(respondents: list, cutoff: float) -> dict:
idx = 0
while idx < len(respondents):
if respondents[idx]["outcome"] > cutoff:
break
idx += 1
result = respondents[idx] if idx < len(respondents) else {}
print(f"First outlier: {result}")
return resultSo if the while loop exhausts the list, idx equals len(respondents) and I know no outlier exists. That's a clean exit condition.
The loop that knows when to give up. Every data quality check needs that property.
In SPSS I'd sort and scroll. Here I stop the moment I find it and keep the dataset order intact.
Order matters for time-series waves. Sorting scrambles entry order — which may be date of completion, a methodologically meaningful sequence. A while loop with break preserves it while still finding the target.
A while loop runs as long as its condition is True. break exits immediately:
idx = 0
while idx < len(items):
if condition(items[idx]):
break # stop here
idx += 1
# after loop: idx is either the found position or len(items)result = items[idx] if idx < len(items) else {}If idx == len(items), the loop ran out — nothing matched. Return a sentinel like {} to signal "not found" without raising an error.
for loops always complete. while + break stops early — essential when scanning large datasets where the first match is all you need.
Ian is running a quality-control check on wave data and needs the first respondent whose outcome exceeds a cutoff value. Write `find_first_outlier(respondents, cutoff)` that uses a while loop to walk the list and returns the first matching respondent dict, or an empty dict `{}` if none is found.
Tap each step for scaffolded hints.
No blank-editor panic.