Use bool list to retrieve elements from another list - Python
Question: I have this situation:

    
    
    main_list = [12, 10, 30, 10, 11,10, 31]
    get_indices = [1, 0, 1, 1, 0, 0, 0]
    

What I want to do is, extract elements from `main_list` according to the
boolean value in `get_indices`. I tried the following but it is not working :

    
    
    result = main_list[bool(get_indices)]
    print result
    10
    

It should be `12, 30, 10`


Answer: I just came across `compress()` which is best suited for this task.
`compress()` generates an iterator for efficient looping.

    
    
    from itertools import compress
    
    main_list = [12, 10, 30, 10, 11,10, 31]
    get_indices = [1, 0, 1, 1, 0, 0, 0]
    
    print list(compress(main_list, get_indices))
    [12, 30, 10]
    

