I have a list of strings. I hope to print out the strings in the list that meet a condition. The list is as below:
In [5]: L = ["John and Mary", "Leslie", "Iva and Mark Li"]
I hope to print out each of the strings in L that has an and
in it --
'John and Mary', 'Iva and Mark Li'
I have the following code:
In [6]: def grep(pattern, line):
if pattern in line:
print line
In [7]: [grep("and", I) for I in L]
This returns
John and Mary
Iva and Mark Li
Out[7]: [None, None, None]
What's the right way to do it? Thank you!!