Python · Level 1 Data

List Comprehensions

Master concise list creation syntax — filter and transform sequences in a single readable expression.

  • [expr for x in iterable]
  • Filtering with if clause
  • Nested comprehensions

A list comprehension builds a new list in one readable line — expression first, then iteration, then an optional filter.

nums = [1, 2, 3, 4, 5]

# Basic: square each number
squares = [x**2 for x in nums]
# -> [1, 4, 9, 16, 25]

# With filter: only evens
even_sq = [x**2 for x in nums if x % 2 == 0]
# -> [4, 16]

Create filter_even_squares(numbers) that returns a list of squares of only the even numbers in the input.

solution.py
Test Suites Pending
Stdout