juniorPython

List Comprehensions

Updated May 4, 2026

Short answer

A concise syntax for creating lists.

Deep explanation

A one-line loop to generate a list from an iterable, often including conditional logic for filtering.

Real-world example

Parsing a list of filenames to keep only those ending in '.jpg'.

Common mistakes

  • Nesting multiple comprehensions, which destroys readability.

Follow-up questions

  • Can you use else in a comprehension?

More Python interview questions

View all →