Sorry, I don't know how to set a decent title for this problem.
I have a function that needs to take a list of lists, e.g. ll = [['a', 'b'], ['c'], ['d', 'e', 'f']]
and return ['a', 'c', 'd', 'b', 'e', 'f']
(so, it doesn't just flatten it).
I have a feeling it is not as efficient as it could be.
def func(lol):
'''
Takes a list of lists of varying lengths and creates a single list by taking one by one element from each list respectively. E.g.:
ll = [[1, 2, 3, 4], [5, 6], [7, 8, 9]]
In: func(ll)
Out: [1, 5, 7, 2, 6, 8, 3, 9, 4]
'''
arr = []
while lol:
for list in lol:
try:
arr.append(list.pop(0))
except:
lol= [list for list in lol if list]
return arr
What would be a better/faster way to solve this?