Please note this is not a duplicate of this post because I want to zip more than 2 lists (or at least I cannot easily generalize that post for use here without explicit loops)
I want to find the best performing (in terms of speed) implementation that merges list of lists in a particular way. The input is a list of lists (or tuples), ordered such that the length of the next list is always multiples of the previous one. For example:
a = ['A', 'B', 'C', 'D']
b = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
input_list = [a, b]
The output is a merged list of:
output = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'A', 'E', 'B', 'F', 'C', 'G', 'D', 'H']
That is, the shorter lists (in this case a) all get expanded to the longest list (in this case b) by cycling over itself so that lists will have equal length. Then all lists are merged in a vertical stacking fashion.
Currently I have an implementation that essentially does the following:
step 1 step 2 step 3
====== ======== ======
ABCD ABCDABCD
ABCDEFGH -------> ABCDEFGH ------> AABBCCDDAEBFCGDH
It works but not efficient:
def flatten_list(value):
return sum(value, [])
def group(value):
for idx in reversed(range(1, len(value))):
multiplier = int(len(value[idx]) / len(value[idx - 1]))
if multiplier > 1:
value[idx - 1] = flatten_list([value[idx - 1] for i in range(multiplier)])
return flatten_list(list(zip(*value)))
Is there a faster implementation? Performance is really crucial to my application as the input can be huge. Any suggestions are appreciated!