interleave()
Merge multiple lists into a single list by inserting the next element of each list by sequential round-robin into the new list.
Implementation
Args: arrays: Lists to interleave. Returns: Interleaved list.
Example
interleave([1, 2, 3], [4, 5, 6], [7, 8, 9])
Expected output: [1, 4, 7, 2, 5, 8, 3, 6, 9]
Source Code
def interleave(*arrays: List[T]) -> List[T]:
result: List[T] = []
max_len = max((len(arr) for arr in arrays), default=0)
for i in builtins.range(max_len):
for arr in arrays:
if i < len(arr):
result.append(arr[i])
return result