You are given a list of integers and need to add only the even ones.
This is not a sum-everything problem. Odd numbers should be ignored completely. If the list contains no even values, the answer should be 0. Negative even numbers still count, and they should affect the total in the normal way.
For example, if numbers = [1, 2, 3, 4], the even values are 2 and 4, so the answer is 6. If numbers = [1, 3, 5], there are no even values at all, so the answer is 0. A case like [-2, -4, 5, 7] should return -6 because both negative even numbers are included.
So the task is to filter by parity and return the total of only the even values.
Example Input & Output
Sum of 0, 2, 4.
No even numbers present.
Sum of even numbers 2 and 4.
Algorithm Flow

Solution Approach
This problem is a good example of Python's sum + filtering style. The main job is not complicated algorithmically, but it is important to express the rule clearly: include a number in the total only if it is even.
In Python, an integer is even when num % 2 == 0. That condition works for positive numbers, zero, and negative numbers too, which is useful because the test data includes cases like -2 and -4.
The cleanest solution is:
This generator expression walks through the list, keeps only the even values, and feeds them into sum(). Because sum() starts from zero by default, an empty input list or a list with no even numbers naturally returns 0 without needing an extra special case.
You could also solve this with a loop:
That version is also correct and may feel more explicit to newer learners. The generator-expression version is simply more compact and idiomatic in Python when the goal is "sum the values that match a condition."
One nice point about the generator version is that it does not build a temporary list of even numbers first. It sends matching values to sum() one by one. For a small input, that difference is not dramatic, but it is still a cleaner expression of the idea.
Another reason this form works well is that the logic stays tightly connected to the problem statement. We are not separating "find the even numbers" and "add them" into far-apart pieces of code. The whole rule is visible in one expression, which makes it easy to review and hard to misread.
The runtime is O(n) because each number is checked once, and the extra space is O(1) beyond the input when using the generator form. The key idea is simple but important: filter the numbers logically, then reduce them into one total. In Python, combining those two steps in one expression is often the clearest solution.
Best Answers
def sum_even_numbers(numbers):
# Approach 1: Generator expression within continuous sum
return sum(n for n in numbers if n % 2 == 0)Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
