I have a list of lists and strings and would like to append the string elements into the list element directly before it. Below is a sample of the data.
data = [['way','say','may','lay'], 'wake', ['hay','pay','yay'], 'lake', ['tay'], 'shake']
The desire output should be something like this:
out = [['way','say','may','lay','wake'], ['hay','pay','yay','lake'], ['tay', 'shake']]
I have tried converting the list into a dataframe and use groupby and cumsum() however this seems to only partly solve my problem.
Thanks!
>Solution :
You could zip together the odd and even parts of data and use list addition of those parts to build your desired output:
out = [odd + [even] for odd, even in zip(data[0::2], data[1::2])]
Output:
[
['way', 'say', 'may', 'lay', 'wake'],
['hay', 'pay', 'yay', 'lake'],
['tay', 'shake']
]