Interview Question

What is list comprehension?

A list comprehension builds a list from concise iteration and filtering.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

A list comprehension creates a list from an expression and one or more for clauses. • Optional if clauses filter values. • Its iteration variables have comprehension-local scope in Python 3. • Prefer a normal loop when the logic becomes difficult to read.

💡 Simple Example

squares = [number * number for number in range(5)] print(squares)

Output

[0, 1, 4, 9, 16]

⚡ Quick Revision

A list comprehension builds a list from concise iteration and filtering.