Filtering out employees that do not meet some criteria, using list comprehensions Right now I have a list of dictionaries. In order to filter out all the entries I do not want included in the list, I am running it through multiple list comprehensions.

Is there a more Pythonic or better way to write these comprehensions that seem repetitive?

#FILTER OUT ALL EMPLOYEES WHO WILL NOT BE FACTORED INTO METRICS
inScope = [s for s in set2 if s['LEAD_TIME'] &gt;= 0]#lead times 0 or more
set2 = inScope

inScope = [s for s in set2 if s['WRK_RLTN_DSC'] != "INDEPENDENT CONTRACTOR"]#not independent contractor
set2 = inScope

inScope = [s for s in set2 if s['WRK_RLTN_DSC'] != "OUTSIDE CONSULTANT"]#not outside consultant
set2 = inScope

inScope = [s for s in set2 if s['WRK_RLTN_DSC'] != "OUTSIDE CONTRACTOR"]#not outside contractor
set2 = inScope

inScope = [s for s in set2 if s['WRK_RLTN_DSC'] != "EMP OF STRATEGIC INVESTMENT CO"]#not emp of strategic investment co
set2 = inScope

inScope = [s for s in set2 if s['BUS_GRP_CD'] != "FIL"]#In scope after removal of FIL Int'l Associates
set2 = inScope
The simplest refactoring is as follows:

CONDITIONS = [
    ('WRK_RLTN_DSC', 'INDEPENDENT CONTRACTOR'), 
    ...
]
set2 = [s for s in set2 if s['LEAD_TIME'] &gt;= 0]
for key, excl in CONDITIONS:
    set2 = [s for s in set2 if s[key] != excl]


Note:


You can assign straight back to set2, getting rid of the temporary inScope variable;
I have factored out the multiple checks into a simple loop over a list of keys and values; and
I have consistently used single quotes for string literals, rather than a mix of single and double.


However, although neater than your current code, this is still relatively inefficient, as it builds multiple lists; you could instead try something like:

set2 = [s for s in set2 if s['LEAD_TIME'] &gt;= 0 and 
        all(s[key] != excl for key, excl in conditions)]


which reduces it to a single list comprehension. An alternative would be to incorporate e.g.:

INVALID_WRK = {
    'INDEPENDENT CONTRACTOR',
    ...
}

... s['WRK_RLTN_DSC'] not in INVALID_WRK ...


as you check that key multiple times.