I'm new to Python. I want to check if the given list A
contains any character among ('0', '2', '4', '6', '8')
or not, where '0' <= A[i] <= '9'
.
I can do this as:
if not ('0' in A or '2' in A or '4' in A or '6' in A or '8' in A):
return False
but, is there any shorter way to do this? Thanks.
You can use any
with generator expression
A = [...]
chars = ('0', '2', '4', '6', '8')
return any(c in A for c in chars)
You can try a for
loop:
for i in '02468':
if i not in A:
return False
If you want True
to be returned if all of the characters are found:
for i in '02468':
if i not in A:
return False
return True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With