How do I loop a python list just in 1 line code?

Question:

I have a list and a string:

exclude_list=['2019-1','2019-2']
first_part='Report - 2019-1'

I need a logic to check ,if any item in the list ,is part of the string ,then just print yes,else print no.

something like:

if any(exclude_list) in first_part:
    pritn('yes')
else:
    print('no')

I can do it this way:

for i in exclude_list:
    if i in first_part:
       print('yes')
    else:
        print('no')

But I want it be simpler, any friend can help ?

Asked By: William

||

Answers:

>>> exclude_list=['2019-1','2019-2']
>>> first_part='Report - 2019-1'
>>> any(e in first_part for e in exclude_list)
True
Answered By: Grisha
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.