Count Negatives
Source: May 2026, Task 2 (DTU 02002/02003). Study solution — not an official answer key.
Problem
Write count_negatives(values) that returns how many integers in the list are strictly negative. Empty list → 0.
Example
>>> count_negatives([3, 8, -2, 0, -10, 17, -1, -1, -2])
5
Approach
Count items where x < 0, e.g. with a generator expression and sum.
Solution
Show solution Hide solution
Solution
def count_negatives(values):
return sum(1 for x in values if x < 0)