Question 11: List Comprehensions to Filter and Transform Lists

This question is to test your ability to use list comprehensions in Python.

You’ll need to understand how to apply conditions and transformations within a list comprehension.

It is an easy question.

Question: List Comprehension

Given the list numbers = [1, 4, 5, 8, 9, 12, 13, 16, 17, 20], which of the following correctly generates a new list containing only the even numbers from the original list, but each number should be squared?

numbers = [1, 4, 5, 8, 9, 12, 13, 16, 17, 20]















Option B is the correct answer.

  • Explanation: This list comprehension filters the numbers list to include only the even numbers (those for which n % 2 == 0), and then squares each of these even numbers (n*n).
  • Result: [16, 64, 100, 784]
  • Reason for Correctness: This option correctly performs both required operations: filtering and squaring even numbers.

Leave a Reply