filter() — Python Technical Interview Questions & Answers
filter()
filter()
- filter() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- filter() — output: [30]. Only values > 20.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix filter up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call filter() once and say what prints.
Code Examples for filter()
Example
# filter()
print(list(filter(lambda n: n > 20, [10, 30, 20])))
filter() — output: [30]. Only values > 20.
How to Explain filter() to an Interviewer
Explain filter() as if you are teaching a junior — definition, then one tiny script.
filter() is built-in — no import. One call + exact output.
What does the example print, and why?
filter() — output: [30]
What mistake do freshers make with filter()?
Wrong type → TypeError.