You could do this:
letter = 'x'
result = [a for a in a_lst if letter in a]
if result:
print('yes', letter)
else:
print('no', letter)
Explanation:
result
will be []
if none of the words in a_lst
has the letter. When you do a if result
on an empty list, it returns False
, otherwise it returns True
. The conditional statements check and print the output statement accordingly.
Another way to do it in python is to use the filter
function:
if filter(lambda x: letter in x, a_lst):
print('yes', letter)
else:
print('no', letter)
Yet another way to do it is to use any
:
if any(letter in word for word in a_list):
print('yes', letter)
else:
print('no', letter)
any(letter in word for word in a_list)
returns True if any of the words have the letter.