0%

Transforming and Combining Data · practice

Check Conditions Across a Collection

sum, min, and max answered numeric questions about the scores in the previous lesson. Other questions about a collection need a True or False answer:

  • Did any learner earn a perfect score?

  • Did all learners complete the quiz?

Python provides any() and all() for exactly this kind of check:

any([False, True, False])  # True
all([True, True, False])   # False

Both receive a collection of values. any returns True when at least one is true, while all returns True only when every value is true.

Build the conditions, then summarize them

We can make each check visible:

perfect_checks = []

for attempt in attempts:
    is_perfect = attempt["score"] == attempt["total"]
    perfect_checks.append(is_perfect)

has_perfect = any(perfect_checks)

Later, comprehensions will make the collection-building step shorter. The key idea here is the question asked by any or all.

Empty collections

any([]) is False: no item satisfies the .

all([]) is True: there is no item that violates the condition. This is logically consistent, but may not match a product rule such as “all learners completed.” If an empty should report False, handle it explicitly.

For this lesson, completion_status([]) returns both results as False because there are no attempts to summarize.

Use if not attempt_list to handle that empty input before calling all().

Which expression answers "Did at least one check pass?"

Report perfect and completed attempts

An attempt is complete when its response is not None. Build the two Boolean collections, then use any and all.

Task

Complete completion_status.

  • Return {"has_perfect": False, "all_complete": False} for no attempts.

  • Build a of checks for score == total.

  • Build a list of checks for response is not None.

  • Use any for has_perfect.

  • Use all for all_complete.

Return the two results in a .