Monday, September 28, 2026

Python List Comprehension Made Easy: Square Only the Even Numbers

 

Python List Comprehension Made Easy: Square Only the Even Numbers

https://technologiesinternetz.blogspot.com


List comprehension is one of Python’s most useful features for creating a new list from an existing collection. It allows developers to combine filtering and transformation into a compact, readable expression.

A common interview-style problem is to take a list of numbers, identify the even values, and then calculate their squares.

Let’s understand how to solve it step by step.

The Problem

Suppose we have the following list:

numbers = [1, 5, 2, 8, 3, 7]

The goal is to:

  1. Find the even numbers.
  2. Calculate the square of each even number.
  3. Store the results in a new list.

The expected result is:

[4, 64]

Why?

  • 2 is even → 2² = 4
  • 8 is even → 8² = 64

The other numbers are odd, so they are ignored.

The Python Solution

numbers = [1, 5, 2, 8, 3, 7]

squares_even = [x**2 for x in numbers if x % 2 == 0]

print(squares_even)

Output:

[4, 64]

Understanding the List Comprehension

The most important part of the program is:

[x**2 for x in numbers if x % 2 == 0]

It may look complicated at first, but we can break it into three simple parts.

1. for x in numbers

for x in numbers

This goes through every number in the list one by one.

The values are:

1
5
2
8
3
7

2. if x % 2 == 0

This checks whether the number is even.

The % operator gives the remainder after division.

For example:

2 % 2 = 0
8 % 2 = 0

Therefore, 2 and 8 are even.

But:

1 % 2 = 1
5 % 2 = 1
3 % 2 = 1
7 % 2 = 1

These numbers are odd and are skipped.

3. x**2

The ** operator is used for exponentiation in Python.

So:

x**2

means:

x × x

For the selected numbers:

2**2 = 4
8**2 = 64

Therefore, the final list becomes:

[4, 64]

The Same Logic Without List Comprehension

Understanding the longer version can make the one-line solution easier to remember.

numbers = [1, 5, 2, 8, 3, 7]

squares_even = []

for x in numbers:
    if x % 2 == 0:
        squares_even.append(x**2)

print(squares_even)

This produces the same output:

[4, 64]

The list comprehension simply puts this logic into a more compact form.

A Simple Formula to Remember

A useful pattern for this type of problem is:

[new_value for item in list if condition]

For this example:

[x**2 for x in numbers if x % 2 == 0]

You can think of it as:

Take → Check → Transform

  • Take: each number from numbers
  • Check: is it even?
  • Transform: square it

Why List Comprehension Is Useful

List comprehensions are particularly useful when working with lists of data. They can make straightforward filtering and transformation operations shorter and easier to read.

For example, you can use them to:

  • Filter values from a dataset
  • Transform numbers
  • Extract specific values
  • Clean simple collections
  • Prepare data for further processing

However, shorter code is not automatically better. If a list comprehension becomes complicated, a normal for loop may be easier for other developers to understand.

Another Example

Suppose we have:

numbers = [2, 4, 5, 7, 10]

We want the squares of even numbers:

result = [x**2 for x in numbers if x % 2 == 0]

print(result)

Output:

[4, 16, 100]

Here, 2, 4, and 10 are even, so only those numbers are squared.

Interview Tip

If an interviewer asks you to use list comprehension for a filtering-and-transformation problem, first identify these three things:

1. What should be selected?

Example:

if x % 2 == 0

2. What transformation should be performed?

Example:

x**2

3. What is the original list?

Example:

numbers

Then combine them:

[x**2 for x in numbers if x % 2 == 0]

Conclusion

Python list comprehension provides a concise way to filter and transform data. In this example, the expression checks every number, keeps only the even values, squares them, and creates a new list.

The complete solution is:

numbers = [1, 5, 2, 8, 3, 7]

squares_even = [x**2 for x in numbers if x % 2 == 0]

print(squares_even)

Output:

[4, 64]

This simple example is also a good introduction to a broader Python programming pattern: filter data based on a condition and transform the values that pass the filter.

When AI Meets Mathematics: How Machines Could Change the Way We Discover and Understand Math

  When AI Meets Mathematics: How Machines Could Change the Way We Discover and Understand Math Mathematics has traditionally been a human-d...