List Comprehensions and Generators
Implement or sketch code for List Comprehensions and Generators. Explain the logic, complexity, and pros/cons of this approach.
Answers use simple, clear English.
Quick interview answer
Logic: [f(x) for x in iterable if cond] builds lists concisely. Generator expressions ( ...
Detailed answer
Logic: [f(x) for x in iterable if cond] builds lists concisely. Generator expressions ( ... ) are lazy and memory-efficient. Dict/set comps follow same syntax. Complexity notes included in code section when present. Pros: Readable for simple transforms; often faster than manual append loops. Cons: Nested comps hurt readability; eager list comp on huge data wastes RAM. Core: [f(x) for x in iterable if cond] builds lists concisely. Generator expressions ( ... ) are lazy and memory-efficient. Dict/set comps follow same syntax. Real-time example: Parse log lines: active_errors = [ln for ln in lines if 'ERROR' in ln and ln.strip()]. Pros: Readable for simple transforms; often faster than manual append loops. Cons: Nested comps hurt readability; eager list comp on huge data wastes RAM. Common mistakes: Side effects inside comprehension; shadowing loop variable leaks in Py2 (not Py3). Best practices: Keep one logical clause; switch to generator for streaming pipelines. Audience level: Junior.
Full explanation
[f(x) for x in iterable if cond] builds lists concisely. Generator expressions ( ... ) are lazy and memory-efficient. Dict/set comps follow same syntax.
Real example & use case
Parse log lines: active_errors = [ln for ln in lines if 'ERROR' in ln and ln.strip()].
Pros & cons
Pros: Readable for simple transforms; often faster than manual append loops. Cons: Nested comps hurt readability; eager list comp on huge data wastes RAM.
Code example
nums = range(10)
squares = [n * n for n in nums if n % 2 == 0]
total = sum(n * n for n in nums if n % 2) # generator — no intermediate list
print(squares, total)Practice code · python (view only · no execution)
nums = range(10)
squares = [n * n for n in nums if n % 2 == 0]
total = sum(n * n for n in nums if n % 2) # generator — no intermediate list
print(squares, total)Common mistakes
Side effects inside comprehension; shadowing loop variable leaks in Py2 (not Py3).
Best practices
Keep one logical clause; switch to generator for streaming pipelines.
Follow-up questions
- How would you test List Comprehensions and Generators?
- What metrics prove List Comprehensions and Generators is healthy in prod?
- How does List Comprehensions and Generators change at 10× traffic?