I found some code I needed in this answer https://stackoverflow.com/a/37401376/1727657. But I don't understand what next()
does in this context. Could someone explain?
Here's a short test script I made to understand it. The idea is to see whether the test string txt
contains any of the strings in myset
and, if so, which one. It works but I don't know why.
myset = ['one', 'two', 'three']
txt = 'A two dog night'
match = next((x for x in myset if x in txt), False)
if match: #if match is true (meaning something was found)
print match #then print what was found
else:
print "not found"
My next question will be to ask whether next()
will give me the index of match
(or do I need to do find()
on txt
)?