Code Logo

Filter and Square Even Numbers

Published at13 Jan 2026
Easy 13 views
Like3

You are given a list of integers and need to build a new list from it in two steps.

First keep only the even numbers. After that, square each number that survived the filter. The output should preserve the original left-to-right order of those even values. That means you are not sorting anything, and you are not changing odd numbers into something else. Odd numbers are simply ignored.

For example, if the input is [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], the even numbers are 2, 4, 6, 8, 10, and their squares are 4, 16, 36, 64, 100. So the final answer is [4, 16, 36, 64, 100]. If the input is [2, 4, 6, 8], then every number is kept and the result is [4, 16, 36, 64]. If the input is [1, 3, 5, 7, 9], the answer is an empty list because no number passes the even filter.

This is a simple transformation pipeline: filter first, then square, then return the collected results as a list.

Example Input & Output

Example 1
Input
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Output
[4, 16, 36, 64, 100]
Explanation

Filter even numbers (2, 4, 6, 8, 10) and square them.

Example 2
Input
[2, 4, 6, 8]
Output
[4, 16, 36, 64]
Explanation

Every value is even, so each one is squared and kept in the same order.

Example 3
Input
[1, 3, 5, 7, 9]
Output
[]
Explanation

No even numbers, so return empty list.

Algorithm Flow

Recommendation Algorithm Flow for Filter and Square Even Numbers
Recommendation Algorithm Flow for Filter and Square Even Numbers

Solution Approach

This problem is a very natural fit for a Python list comprehension because the task is really one clean pipeline: scan the input, keep only the values that match a condition, transform those values, and collect everything into a list.

The condition here is "is the number even?" In Python, an integer is even when num % 2 == 0. Once a number passes that check, the transformation is just its square, which can be written as num * num or num ** 2.

That means the whole solution can be written compactly as:

return [num * num for num in numbers if num % 2 == 0]

This reads from left to right in the same order as the problem statement. We iterate through numbers, we keep only the even values, and for each kept value we place its square into the result list.

The important detail is the order of operations. The if part is acting like the filter. It removes odd numbers completely. The expression at the beginning of the comprehension is the transformation step, so only the even numbers get squared and returned.

You could solve this with a normal for loop too. For example, you could create an empty list, check every number, and append num * num whenever the number is even. That version is perfectly correct. But in Python, list comprehensions are usually preferred for short filter-and-transform tasks because they keep the whole idea in one place. The code feels closer to the problem statement itself.

Another useful detail is that this approach preserves order automatically. Since the comprehension walks through the input sequence from left to right, the squared results appear in the same relative order as the original even numbers. That matters because the problem is about transformation, not reordering.

This approach runs in O(n) time because each input value is checked once. The extra space is proportional to the number of even values collected into the output list. It is concise, readable, and very idiomatic for this exact kind of Python task.

Best Answers

python - Approach 1
def filter_and_square_even(numbers):
    return [n * n for n in numbers if n % 2 == 0]