I am going to realize the following functions: k is a list, e.g.:
k = [1,2,3,4,5,6,7,6,4,7,2,3,4,4,8,9,1,0,2,4]
I want to have a list of boolean values if the elements is in a subset of this list, e.g. if element in [2,5,9], return true, else false:
p = [False, True, False, False, True, False, False, False, False, False, True, False, False, False, False, True, False, False, True, False]
I am only capable of achieving this by standard for loops:
p = []
for element in k:
if element in [2,5,9]:
p.append(True)
else:
p.append(False)
However, what I want is to achieve this with a simple short-hand statements, then I wrote:
p2 = [True for i in k if i in [2,5,9] else False]
But this gives an error upon 'else':
File "<stdin>", line 1
p2 = [True for i in k if i in [2,5,9] else False]
^
SyntaxError: invalid syntax
So how can I correct this?