Skip to content
C

Python Interview Questions

Iterators, Generators & Functional Tools Interview Questions

Iterables, iterators, generators, and the built-in functional tools -- enumerate, zip, map, filter, reduce.

Question 1: What is an iterable?

Ans

An iterable is an object that can provide an iterator, allowing it to be traversed with a for loop. Lists, tuples, strings, dictionaries, sets, and ranges are common iterables.

Example

python
for x in [1, 2, 3]: print(x)

Important Point

An iterable is not necessarily itself an iterator.

Question 2: What is an iterator?

Ans

An iterator implements the iterator protocol, providing __iter__() returning itself and __next__() returning the next value or raising StopIteration.

Example

python
it = iter([10, 20]) print(next(it)) print(next(it))

Important Point

An iterator is stateful and is normally consumed as values are requested.

Question 3: What is a generator?

Ans

A generator is a lazy iterator commonly created with a function containing yield or a generator expression. It produces values on demand instead of building the entire result at once.

Example

python
def numbers(): yield 1 yield 2 for n in numbers(): print(n)

Important Point

Generators are useful for large or streaming data because they can keep memory usage low.

Question 4: yield vs return?

Ans

return ends a normal function and provides one final result. yield pauses a generator function and preserves its execution state so it can continue later.

Example

python
def count(): yield 1 yield 2 print(list(count()))

Important Point

A function containing yield returns a generator iterator when called.

Question 5: Generator expression vs list comprehension?

Ans

A list comprehension creates the complete list immediately. A generator expression produces values lazily as they are iterated.

Example

python
list_values = [x*x for x in range(5)] gen_values = (x*x for x in range(5)) print(list_values) print(list(gen_values))

Important Point

Use generators when immediate materialization is unnecessary or the input is large.

Question 6: What is enumerate()?

Ans

enumerate() produces pairs containing an index and an item while iterating, avoiding manual counter management.

Example

python
for index, name in enumerate(["A", "B"], start=1): print(index, name)

Important Point

The start value changes the reported index; it does not modify the underlying sequence.

Question 7: What is zip()?

Ans

zip() combines elements from multiple iterables into tuples, stopping when the shortest input is exhausted by default.

Example

python
names = ["A", "B"] marks = [80, 90] for name, mark in zip(names, marks): print(name, mark)

Important Point

Modern Python also provides `zip(..., strict=True)` when unequal lengths should raise an error.

Question 8: What is map()?

Ans

map() applies a function to each item of an iterable and returns a lazy map iterator in Python 3.

Example

python
values = map(str, [1, 2, 3]) print(list(values))

Important Point

A comprehension is often easier to read when the transformation is simple.

Question 9: What is filter()?

Ans

filter() lazily keeps elements for which a predicate is true.

Example

python
values = filter(lambda x: x % 2 == 0, range(6)) print(list(values))

Important Point

Comprehensions can be clearer for many filtering tasks.

Question 10: What is reduce()?

Ans

functools.reduce() repeatedly combines iterable values using a binary function to produce one accumulated result.

Example

python
from functools import reduce print(reduce(lambda a, b: a + b, [1, 2, 3, 4], 0))

Important Point

For common operations such as sum, prefer the built-in function when available.

Question 11: What is range()?

Ans

range() represents an arithmetic progression of integers and produces values lazily when iterated.

Example

python
for i in range(1, 5): print(i)

Important Point

The stop value is excluded, and range objects support efficient membership/index-related operations without storing every integer.

Question 12: Why use enumerate instead of range(len())?

Ans

enumerate() directly provides both the item and its index, making iteration clearer and avoiding manual indexing.

Example

python
for i, value in enumerate(values): print(i, value)

Important Point

Use range(len(...)) when you genuinely need index-based mutation or multiple indexed accesses.

Question 13: What is enumerate start parameter?

Ans

The start argument controls the initial index returned by enumerate without changing the underlying iterable.

Example

python
for i, value in enumerate(["A", "B"], start=100): print(i, value)

Important Point

It is useful when displayed numbering should begin at a domain-specific value.

Question 14: What is zip strict mode?

Ans

zip(..., strict=True) raises ValueError if the input iterables do not have equal lengths, helping detect silent data truncation.

Example

python
for a, b in zip([1,2], [10,20], strict=True): print(a, b)

Important Point

Use strict mode when unequal lengths indicate a data error.

Continue Your Preparation