I have a python list in which i need to do takewhile. I am getting output as
['fd', 'dfdf', 'keyword', 'ssd', 'sdsd']
but I need to get ['3=', 'fd', 'dfdf', 'keyword', 'ssd', 'sdsd', ';']
from itertools import takewhile, chain
l = [1, 2, "3=", "fd", "dfdf", "keyword", "ssd", "sdsd", ";", "dds"]
s = "keyword"
# get all elements on the right of s
right = takewhile(lambda x: ';' not in x, l[l.index(s) + 1:])
# get all elements on the left of s using a reversed sublist
left = takewhile(lambda x: '=' not in x, l[l.index(s)::-1])
# reverse the left list back and join it to the right list
subl = list(chain(list(left)[::-1], right))
print(subl)
# ['fd', 'dfdf', 'keyword', 'ssd', 'sdsd']