Question
Python 3.x You are to write an iterator that will output tuples of two elements from the given lists. Given the class Pair you will
Python 3.x
You are to write an iterator that will output tuples of two elements from the given lists.
Given the class Pair you will need to implement the following methods:
__iter__ this will return an iterator
__next__ returns the next element
__init__ initialises the iterator class or
You may create a separate iterator class after __iter__ is called.
When next() is called with the iterator should return the next pair of elements. If the number of elements are uneven, the position the smaller list's elements are place would be replaced with None.
Once the iterator has finished returning elements, your iterator should raise StopIteration
SCAFFOLD:
Example 1 k -[0, 9, 8, 7, 6] pair - iter(Pair(1, k)) #Checking if print (next(pair)) #(1, e) print(next(pair)) #(2, 9) Example 2 k [0, 9, 8] pair -iter(Pair(1, k)) print (next(pair)) #(1, 0) print (next(pair)) #(2, 9) print(next(pair)) #(3, 8) print(next(pair)) #(4, None)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started