jeudi 18 juillet 2019

How to slice a list of strings till index of matched string depending on if-else condition

I have a list of strings =

['after','second','shot','take','note','of','the','temp']

I want to strip all strings after the appearance of 'note'. It should return

['after','second','shot','take']

There are also lists which does not have the flag word 'note'.

So in case of a list of strings =

['after','second','shot','take','of','the','temp']

it should return the list as it is. How to do that in a fast way? I have to repeat the same thing with many lists with unequal length.

 tokens = [tokens[:tokens.index(v)] if v == 'note' else v for v in tokens]

Aucun commentaire:

Enregistrer un commentaire