aBool = bool(aList.index(aVal))
I tried this but it gives an error for false. Any help is appreciated!
aBool = bool(aList.index(aVal))
I tried this but it gives an error for false. Any help is appreciated!
aVal in aList
The in
operator does what you need, if I understand correctly.
There are different ways you could approach this problem but the easiest would be to use a conditional with the 'in' operator. For example,
#testbool will hold your boolean value
#testlist will be your list
#testvar will be your variable you are checking the list for
if testvar in testlist:
testbool = true
else:
testbool = false
Here's a general solution to it:
_list = range(10)
aVal = 7
aBool = (lambda val, l: True if val in l else False)(aVal, _list)
print(aBool)
You can pass any value to the first argument, and any list to the second argument, and it'll always return True if the value is inside the given list (And just like you want, return False if otherwise).
Use testvar in testlist
will return a boolean value.
If you want to reuse the value, you can do as below:
aTest = aVal in aList