Stackoverflow, hello,
I have very nervous question for me, which I can't solve for several hours now.
Task is:
I have a list:
['Egg', 'Milk', 'Tomato', '_', 'Duck', 'Water', 'Honey', 'Soy', '_', 'Potato', 'Garlic', 'Gouda', '_', 'Beef', 'Sweet', 'Pita', 'Wine', 'Tomato']
And I need to transfrom it into new one, where all the ingridients between '_' will be as a one element:
a= [['Egg', 'Milk', 'Tomato'], ['Duck', 'Water', 'Honey', 'Soy'], ['Potato', 'Garlic', 'Gouda'], ['Beef', 'Sweet', 'Pita', 'Wine', 'Tomato']].
However, the difficult part here, is I may have more "small lists" inside the initial list.
For example, after last 'Tomato' it could be a '_' also, and after that - ext few ingridients like ['Bread', 'Milk', 'Egg'], for example.
And the initial list could be look like:
['Egg', 'Milk', 'Tomato', '_', 'Duck', 'Water', 'Honey', 'Soy', '_', 'Potato', 'Garlic', 'Gouda', '_', 'Beef', 'Sweet', 'Pita', 'Wine', 'Tomato', '_', 'Bread', 'Milk', 'Egg']
So I figured out that I MUST NOT make manually 4 different lists and than join them, because there could be 5 or more lists inside the big one, separated by '_'. I must do something else. But all I've got is wrong stuff:
for elem in ingridient_names:
i=1
if elem != '_':
ingridient_names_final[i].append(elem)
print(ingridient_names_final)
Could somebody help, please?