Question: 2 . Iterators: Write a function that takes two equal - length lists as input and returns an iterator. The iterator should yield elements from
Iterators: Write a function that takes two equallength lists as input and returns an iterator.
The iterator should yield elements from the two lists alternately without creating a new list
to store the combined elements. Implement the solution using iterators, such as a for loop
or the next method."
pg
For example:
input:
list
listabcd
output:
abcd
Hint: An iterator is an object that implements iter which is expected to return
an iterator object. An iterator object implements next which is expected to return the
next element of the iterable object that returned it
Implement a function that takes two lists of equal length as input. The objective is to solve
the problem using iterators, such as a for loop or the next method, instead of creating a
new list to store the combined elements of the two lists. The function should return an
iterator that yields the elements from the two lists alternately. It should iterate through the
lists and yield one element from the first list, then one element from the second list, and so
on until all elements have been exhausted.
Put your Solution here:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
