I have this
s = ['son','abc','pro','bro']
b = ['son','bro']
c = ['pro','quo']
The expected output is this. Where items in the output are index(item_in_s)
if it is present in list b
. Or index(item_in_s)+10
if an item is in c
.
[0,12,3]
I tried this:
index_list = [s.index(item) if item in b else s.index(item)+10 if item in c for item in s]
print(index)
But apparently this is a syntax error. So I tried this:
index_list = [s.index(item) if item in b else s.index(item)+10 for item in s if item in c]
print(index)
Output:
[12]
This just changes the whole logic. Although I could do this
fin = [s.index(item) if item in b else s.index(item)+10 if item in c else '' for item in s]
fin = [item for item in fin if item!='']
print(fin)
Desired output obtained:
[0, 12, 3]
But how do I obtain what I want in list comprehension itself or is there something like else continue
in list comprehensions?