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.