A label list that stops at assert

Code review · Medium · §10.3, §10.6 Polishing the math helper

This code labels each like count "popular" or "normal" twice: first with a loop (old, the reference), then with a one-line comprehension. Then it uses assert to check that the two results match. When you run it, it stops at the assert line with an AssertionError. Fix just the comprehension line so that the code passes the assert.

Code to fix

likes = [15, 100, 240, 0]
old = []
for n in likes:
    if n < 100:
        old.append("normal")
    else:
        old.append("popular")

new = ["normal" if n >= 100 else "popular" for n in likes]
assert old == new
print(new)

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.