Is this what you’re looking for? To initialize the list with the first keyword
for row in range(len(locations_and_xy)):
if keywords[0] in locations_and_xy[row]:
new_locations_and_xy = locations_and_xy[row:]
Then we can do this
for i in new_locations_and_xy:
print ([i for j in keywords if j in i])
[['Add', '1215', '697']]
[['to', '1241', '698']]
[['Cart', '1268', '697']]
Edit
To give an update on the comment, I have tried out a new list:
locations_and_xy = [
['to', '451', '851'],
['and','3','3212'],
['Add', '1215', '697'],
['Hi','1223','232'],
['Cart', '1268', '697'],
['to', '1241', '698'],
['Hello','3233','3']
]
keywords = ['Add', 'to', 'Cart']
This may be a better approach to getting the desired output according to the keywords if the list has many other inputs.
for row in range(len(locations_and_xy)):
if keywords[0] in locations_and_xy[row]:
new_locations_and_xy = locations_and_xy[row:]
for i in keywords:
print ([j for j in new_locations_and_xy if i in j])
[['Add', '1215', '697']]
[['to', '1241', '698']]
[['Cart', '1268', '697']]
5
solved How to find consecutive strings in nested lists [closed]