I need to make a "generator function" which will take 2 lists and concatenate numbres from two lists by indices into a tuple. For example:
l1 = [3, 2, 1]
l2 = [4, 3, 2]
The result of the first iteration will be
(3, 4)
The result of the second iteration will be
(2, 3)
And the third
(1, 2)
And also, one of lists may have more numbers than the other one. In that case I need to write condition "if one of the lists ended while iterating, then no further iterations are performed." (using try, except
)
I know, that generator functions use yield
instead of return
, but I have no idea how to write this fuction...
I did this
def generate_something(l1, l2):
l3 = tuple(tuple(x) for x in zip(l1, l2))
return l3
output is
((3, 4), (2, 3), (1, 2))
It works, but this is not geterator function, there is no yield
, there is no first-,second-,etc- iterations. I hope you can help me...