python - Get elements from one list based on another boolean list -


i have 2 lists of same size, 1 list of strings, , other list of booleans (true, false) , want return list of strings if index true.

b_list = [true, false, true] s_list = ['abc', 'sfsfsfsf', 'def'] 

want

s_list = ['abc','def']  

use itertools.compress

compress(data, selectors): return data elements corresponding true selectors elements

so s_list data , b_list selectors:

in [8]: import itertools  in [9]: list(itertools.compress(s_list, b_list)) out[9]: ['abc', 'def'] 

Comments