🐍 Python Beginner

What are Python list comprehensions?

Why Interviewers Ask This

Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Python basics — a prerequisite for any developer role.

Answer

A list comprehension provides a concise way to create a new list by applying an expression to each item in an iterable, optionally filtering items with a condition. Syntax: [expression for item in iterable if condition]. Examples: squares = [x**2 for x in range(10)], evens = [x for x in range(20) if x % 2 == 0], pairs = [(x, y) for x in range(3) for y in range(3)]. List comprehensions are more readable and often faster than equivalent for loops with append(). Similarly, dict comprehensions: {k: v for k, v in items}, set comprehensions: {x**2 for x in range(10)}, and generator expressions (lazy, memory-efficient): (x**2 for x in range(10)).

Common Mistake

Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Python experience.