Filtering a list based on a list of booleans

I have a list of values which I need to filter given the values in a list of booleans:

    list_a = [1, 2, 4, 6]
    filter = [True, False, True, False]

I generate a new filtered list with the following line:

    filtered_list = [i for indx,i in enumerate(list_a) if filter[indx] == True]

which results in:

    print filtered_list
    [1,4]

The line works but looks (to me) a bit overkill and I was wondering if there was a simpler way to achieve the same.

Advices

Summary of two good advices given in the answers below:

1- Don't name a list filter like I did because it is a built-in function.

2- Don't compare things to True like I did with if filter[idx]==True.. since it's unnecessary. Just using if filter[idx] is enough.

You're looking for itertools.compress:

    >>> from itertools import compress
    >>> list_a = [1, 2, 4, 6]
    >>> fil = [True, False, True, False]
    >>> list(compress(list_a, fil))
    [1, 4]

Timing comparisons(py3.x):

    >>> list_a = [1, 2, 4, 6]
    >>> fil = [True, False, True, False]
    >>> %timeit list(compress(list_a, fil))
    100000 loops, best of 3: 2.58 us per loop
    >>> %timeit [i for (i, v) in zip(list_a, fil) if v]  #winner
    100000 loops, best of 3: 1.98 us per loop

    >>> list_a = [1, 2, 4, 6]*100
    >>> fil = [True, False, True, False]*100
    >>> %timeit list(compress(list_a, fil))              #winner
    10000 loops, best of 3: 24.3 us per loop
    >>> %timeit [i for (i, v) in zip(list_a, fil) if v]
    10000 loops, best of 3: 82 us per loop

    >>> list_a = [1, 2, 4, 6]*10000
    >>> fil = [True, False, True, False]*10000
    >>> %timeit list(compress(list_a, fil))              #winner
    1000 loops, best of 3: 1.66 ms per loop
    >>> %timeit [i for (i, v) in zip(list_a, fil) if v] 
    100 loops, best of 3: 7.65 ms per loop

Don't use filter as a variable name, it is a built-in function.

From: stackoverflow.com/q/18665873

Back to homepage or read more recommendations: