print [s for s in list if sub in s]
If you want them separated by newlines:
print "\n".join(s for s in list if sub in s)
Full example, with case insensitivity:
mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'
print "\n".join(s for s in mylist if sub.lower() in s.lower())
Answer from David Robinson on Stack Overflowprint [s for s in list if sub in s]
If you want them separated by newlines:
print "\n".join(s for s in list if sub in s)
Full example, with case insensitivity:
mylist = ['abc123', 'def456', 'ghi789', 'ABC987', 'aBc654']
sub = 'abc'
print "\n".join(s for s in mylist if sub.lower() in s.lower())
All the answers work but they always traverse the whole list. If I understand your question, you only need the first match. So you don't have to consider the rest of the list if you found your first match:
mylist = ['abc123', 'def456', 'ghi789']
sub = 'abc'
next((s for s in mylist if sub in s), None) # returns 'abc123'
If the match is at the end of the list or for very small lists, it doesn't make a difference, but consider this example:
import timeit
mylist = ['abc123'] + ['xyz123']*1000
sub = 'abc'
timeit.timeit('[s for s in mylist if sub in s]', setup='from __main__ import mylist, sub', number=100000)
# for me 7.949463844299316 with Python 2.7, 8.568840944994008 with Python 3.4
timeit.timeit('next((s for s in mylist if sub in s), None)', setup='from __main__ import mylist, sub', number=100000)
# for me 0.12696599960327148 with Python 2.7, 0.09955992100003641 with Python 3.4
Videos
I'm SO close to a solution I can taste it - but still not quite there ;)
I collect an input phrase from the user, and then search for the phrase in each list individually. Depending if the phrase is in the list, it calls a specific function.
But I also need to check if the phrase is in ANY of the lists (and if it's not I'll display an error message). I've arrived at the following:
value = "the date"
status_strings = ["how are you",
"are you ok"]
time_strings = ["what is the time",
"current time",
"time is it"]
date_strings = ["what is today's date",
"what day is it",
"current date",
"the date"]
alarm_strings = ["set an alarm",
"wake me up",
"wake up"]
for x in (status_strings,time_strings,date_strings,alarm_strings):
if value not in x:
print(False)The output for this is of course, False False False - because the value is in one of the lists - but I don't want it to return False for EVERY list that it's not in, but only once, if it's not in ANY of the lists.
Can anyone help me get on the right path?
Edit: The solution was to change the 'for' loop a bit (thanks u/Allanon001):
if not any(value in x for x in (status_strings,time_strings,date_strings,alarm_strings)):
print("BOOYAH")Say I have a list of strings like: ["Hey dude", "Hey bro", "Sup dude", "Hey bud"]
How do I check if *any* of those strings in the list contain the word "Sup"?Alternatively, in my case, I could also check if *all* of the strings contain "Hey" , which I also don't know how to do.
(Sorry, I'm new to Python)
Edit: Thank you so much for all the help, guys <3