nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound
TechnologiesCoding ProblemsBookmarksLearning PathsLogin
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Mock Interviews
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

Questions
2 of 4
1What is a lambda function, and what are its limitations compared to a regular function?
2How would you use `map`, `filter`, and `functools.reduce` together, and when is a comprehension more Pythonic?
3What are list, dict, and set comprehensions, and why are they often preferred over explicit loops?
4What is a generator expression, and how does it differ from a list comprehension in terms of memory usage?
PythonPython
Basics
Control Flow and Functions
Data Structures
Comprehensions & Functional Programming
Iterators, Generators & Decorators
Object-Oriented Programming
Exception Handling & Debugging
Concurrency & Parallelism
Performance & Optimization
Testing
Security
Modules, Packaging & Environment
Type Hinting & Modern Python
System Design & Architecture with Python
Best Practices & Design Patterns
Edge Cases & Tricky Interview Questions
02 / 04

How would you use `map`, `filter`, and `functools.reduce` together, and when is a comprehension more Pythonic?

Difficulty: 5/10
Functional Programming Tools, map, filter and reduce, Comprehension Idioms

map/filter/reduce pipelines vs comprehensions

map(f, it) applies f to each item, filter(pred, it) keeps items where pred is truthy, and functools.reduce(f, it, initial) folds a sequence into a single value by repeatedly applying a two-argument function. In Python 3, map and filter return lazy iterators (not lists), and reduce lives in functools because it was removed from the builtins in 3.0. Chained, they express a classic functional pipeline: filter, then transform, then aggregate.

javascript

When I choose which: for the same filter-plus-transform, a comprehension or generator expression is more Pythonic because it avoids lambdas, reads left to right, and supports conditions and nesting naturally ([f(x) for x in xs if p(x)]). map is still a good fit when the function already exists, especially a C-implemented one like str, int or len (map(int, parts)), or when mapping over several iterables in parallel, and it can be a little faster there because no Python-level lambda is called per item. With a lambda, map and filter are typically no faster than a comprehension, and readability suffers.

reduce is the one I use least, because most reductions already have a clearer name: sum, min, max, any, all, math.prod, str.join, and math.gcd with multiple arguments (3.9+). Legitimate uses are those with no builtin: function composition (above), merging a list of dicts or sets with an operator, or a custom fold with state. Always pass an initial value when the input may be empty, or you get a TypeError; and avoid reduce with string or list concatenation in a loop-like way because it creates quadratic work. Guido van Rossum famously argued for removing it from the builtins for readability reasons, and style guides typically favour explicit loops or builtins over complex reduce lambdas.

Other practical points: because map and filter are lazy, you must consume them (list(), for loop) and they are single-pass, like generators. For parallel work, multiprocessing.Pool.map and concurrent.futures.Executor.map have the same shape but require picklable functions, so lambdas fail there. The operator module (itemgetter, attrgetter, add, mul) and functools.partial replace many lambdas and are both faster and clearer.

Scenario Questions

0-2 years experience

  1. 1What does map(str, [1, 2, 3]) return in Python 3, and how do you see the values?
  2. 2Rewrite list(filter(lambda x: x > 2, nums)) as a comprehension.

2-5 years experience

  1. 1Why does reduce(operator.add, []) raise a TypeError, and how do you prevent it?
  2. 2Given sum, max, min, any, all, math.prod and str.join, when would you still reach for reduce?

5-8 years experience

  1. 1In code review you see map(lambda ..., filter(lambda ..., data)) chained for a data transform. How would you refactor it, and how does performance change when the function is a builtin rather than a lambda?
  2. 2Implement compose(f, g, h) using reduce. How do you handle empty input, and what does it do to stack traces and debugging?

8+ years experience

  1. 1You must process a billion records. Compare lazy map and filter, comprehensions, itertools and multiprocessing.Pool.map in terms of memory, ordering and pickling requirements.
  2. 2Why can't lambdas be used with multiprocessing.Pool.map, and which alternatives (named functions, functools.partial, operator helpers) work?

Follow-up Questions

  • Why was reduce moved out of the builtins in Python 3?
  • Why can't you pass a lambda to multiprocessing.Pool.map?
Sharethis question

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.