The top answer to Interleave multiple lists of the same length in Python uses a really confusing syntax to interleave two lists l1
and l2
:
l1 = [1,3,5,7]
l2 = [2,4,6,8]
l3 = [val for pair in zip(l1, l2) for val in pair]
and somehow you get l3 = [1,2,3,4,5,6,7,8]
I understand how list comprehension with a single "for z in y
" statement works, and I can see that somehow we're looping through the zip, and within that through each tuple. But how does that turn a list of four tuples into an interleaved list of eight? Totally bewildered by this line of code.