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 1
A comprehension can filter:[expr for x in nums if condition].Hint 2
x % 2 == 0 is true for even numbers, including 0 and negative ones (in Python, -4 % 2 is 0).Learn the concepts
- Comprehensions · 5 min read. List, dict and set comprehensions, filtering versus choosing, nesting, and generator expressions.
More Python problems
- Flatten a Nested List · Easy
- Top K Frequent Words · Easy
- Invert a Dictionary · Easy
- Merge Dicts and Sum Shared Keys · Easy
- Group Records by Key · Easy
- Count Words in Reviews · Easy
- Zip Columns into Records · Easy
- Find the Missing IDs · Easy
- Safe Min and Max · Easy
- Sort by Several Keys · Medium
- Fix the Mutable Default Argument · Medium
- Override a Config Without Changing It · Medium
- Keep the Last N Events · Medium