The Python Oracle

Is it possible to find out which condition is being met in a multi condition if then statement?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Cosmic Puzzle

--

Chapters
00:00 Is It Possible To Find Out Which Condition Is Being Met In A Multi Condition If Then Statement?
00:33 Answer 1 Score 2
00:46 Accepted Answer Score 2
01:08 Thank you

--

Full question
https://stackoverflow.com/questions/4844...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #beautifulsoup

#avk47



ANSWER 1

Score 2


[EDIT: changed to only find first name]

for name in ('tribe', 'ailec', 'tfly'):
    if name in entry:
        print ('Name =', name)
        print('Plugin Found!')
        rating = easy
        sheet.cell(row=i, column=12).value = rating
        break



ACCEPTED ANSWER

Score 2


I would use a generator comprehension that I would pass to next with a default value. If the comprehension doesn't find anything, next returns the default value, else it returns the first found and stops there (kind of any, but which stores the result)

cases = ['tribe','allec','tfly']
entry = 'iiii allec rrrr'


p = next((x for x in cases if x in entry),None)
if p is not None:
    print('Plugin Found!',p)