Skip to content

2. Squares of the Even Numbers

Difficulty: Easy · Topic: Core Python & data structures

Write even_squares(nums): given a list of integers, return the squares of the even ones, in their original order.

Zero and negative even numbers count as even. Try to write it as a single list comprehension.

Your code runs in CPython on our servers against 5 tests, including hidden ones.

Sample test

even_squares([1, 2, 3, 4, 5, 6])

Expected: [4, 16, 36]

Hints

Hint 1A comprehension can filter: [expr for x in nums if condition].
Hint 2x % 2 == 0 is true for even numbers, including 0 and negative ones (in Python, -4 % 2 is 0).

Learn the concepts

More Python problems

All Python problems