I am expected to combine multiple lists in Python as follows:
combine(['a', 'b', 'c'], [1, 2, 3]) == ['a', 1, 'b', 2, 'c', 3]
combine(['a', 'b', 'c'], [1, 2, 3, 4, 5]) == ['a', 1, 'b', 2, 'c', 3, 4, 5]
combine(['a', 'b', 'c'], [1, 2, 3, 4, 5], [6, 7], [8]) == ['a', 1, 6, 8, 'b', 2, 7, 'c', 3, 4, 5]
def combine(*args):
Length = []
L = []
for i in range(len(args)):
Length.append(len(args[i]))
i = 0
for k in range(len(args[i])):
for i in range(len(args)):
if not (args[i][k]):
pass
else:
L.append(args[i][k])
return L
I've tried to run this query. It does work if the size of the arrays are equal. However, if there is a list has size less than others "[1]". It gives an error follows:
list index out of range
How can I handle it? Thanks.