Q:

Python: Elegant solution to check if any substring is in list? 

Before you close this, please try to understand the question - it's a bit different, but looks like a lot of downvoted questions.  
I'm looking for a simple way to check if any substrings of a string is in a list of strings.  The input is like 2018_JAN_BGD, and I want to convert the month info in this to a (year-)quarter info.  For example Q1 would be January, February and March.  So I'm looking for a way to check if any of the substrings in 2018_JAN_BGD contains JAN, FEB or MAR.  I could do this by writing 12 (for the whole year) if-else clauses with or's, but I feel like there's a more elegant solution in Python.  I'm trying:
def tellQuarter(month):
    Q1 = ('JAN','FEB','MAR')
    Q2 = ('APR','MAY','JUN')
    Q3 = ('JUL','AUG','SEP')
    Q4 = ('OCT','NOV','DEC')
    if any(for mon in Q1 in month):
        return ("Q1")
    if any(for mon in Q2 in month):
        return ("Q2")

This is of course incorrect (invalid syntax), but I can't figure out how to put this correctly.  Basically, I want to loop through Q1, and check if any of the strings is a substring of month. 
I mean I can do 
for mon in Q1:
    if mon in month:
        return "Q1"

but isn't there a more elegant, single-line solution to this? 

A:

Cheating, but use a dictionary, mapping the months to quarters:
monthsToQuarters = {
    'JAN': 'Q1','FEB': 'Q1','MAR': 'Q1',
    'APR': 'Q2','MAY': 'Q2','JUN': 'Q2',
    'JUL': 'Q3','AUG': 'Q3','SEP': 'Q3',
    'OCT': 'Q4','NOV': 'Q4','DEC': 'Q4'}

giving you:
>>> monthsToQuarters['JAN']
'Q1'

you could strip out the month using something like:
>>> '2018_JAN_BGD'.split('_')[1]
'JAN'

A:

Avoid repeating logic with a dict:
QUARTERS = {
    'Q1': ('JAN', 'FEB', 'MAR'),
    'Q2': ('APR', 'MAY', 'JUN'),
    'Q3': ('JUL', 'AUG', 'SEP'),
    'Q4': ('OCT', 'NOV', 'DEC'),
}

def tellQuarter(month):
    for q, months in QUARTERS.items():
        if any(m for m in months if m in month):
            return q
    raise ValueError("String '{}' does not contain a month. ".format(month))

print(tellQuarter('2018_JAN_BGD'))
>>> Q1

