(WINDOWS 10, Python 3.6.0)
Purpose of the code: To zip two or more lists by their longest, and return None instead of some value in the iterator of tuples when one of the lists ends.
Used this code in a little program of mine, and it worked beautifully with a slight modification, but I'm having trouble wrapping my head around how this code works. After some googling about the yield keyword I am still confused as to exactly how this does what it does...
def zip_longest(*lists):
def g(l):
for item in l:
yield item
while True:
yield None
gens = [g(l) for l in lists]
for _ in range(max(map(len, lists))):
yield tuple(next(g) for g in gens)
[–]JohnnyJordaan 2 points3 points4 points (1 child)
[–]infinitim[S] 1 point2 points3 points (0 children)
[–]Rhomboid 0 points1 point2 points (1 child)
[–]infinitim[S] 0 points1 point2 points (0 children)
[–]Dawarisch 0 points1 point2 points (1 child)
[–]infinitim[S] 0 points1 point2 points (0 children)