itertools — Python Technical Interview Questions & Answers
itertools
itertools
- itertools is about functions that take/return functions, or itertools helpers (chain, cycle, combinations). map/filter/lambda are the fresher versions.
- Keep it concrete: map(str.upper, ["a","b"]) → A B. itertools.chain([1],[2,3]) → 1 2 3.
- itertools — output: [1, 2, 3] then [('A','B'), ('A','C'), ('B','C')].
Technical Interview Tip: One tiny map/filter or itertools line + output.
Code Examples for itertools
Example
# itertools
import itertools
print(list(itertools.chain([1], [2, 3])))
print(list(itertools.combinations("ABC", 2)))
itertools — output: [1, 2, 3] then [('A','B'), ('A','C'), ('B','C')].
How to Explain itertools to an Interviewer
Explain itertools as if you are teaching a junior — definition, then one tiny script.
itertools works with functions or iterators. map/filter or itertools.
What does the example print, and why?
itertools — output: [1, 2, 3] then [('A','B'), ('A','C'), ('B','C')]
What mistake do freshers make with itertools?
Overusing lambda when def is clearer.