QUESTION 8 OF 15

When is a list comprehension preferable to a loop?

Reveal the answer and explore the explanation.

08
Medium

When is a list comprehension preferable to a loop?

Answer
Answer

Compact expression that constructs a list from an iterable with optional filtering.

Practical example

Square only positive values in a list.

Reference: Further reading ↗

Last 30 min revisionHigh-yield interview Q&As