I have two lists:
A: [ True True True False True False True ]
B: ['A', 'B', 'C', 'D', 'E', 'F', 'G']
I want to get only those values from list B where list A is True.
Desired output:
['A', 'B', 'C', 'E', 'G']
You can use itertools.compress(...):
import itertools
a = [ True, True, True, False, True, False, True ]
b = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
ab = itertools.compress(b, a)
print(list(ba))
If you do not want to import itertools, simple list comprehension with zip built-in function would be enough.
conditions = [True, True, True, False, True, False, True]
objects = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
result = [o for o, c in zip(objects, conditions) if c]
assert result == ['A', 'B', 'C', 'E', 'G']
Fairly simple problem to be posted as a question; here is the solution. Would appreciate if you could try it before asking here.
Code:
a = [True, True, True, False, True, False, True]
b = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
res = []
for x in range(len(a)):
if a[x]==True:
res.append(b[x])
print res
Output:
['A', 'B', 'C', 'E', 'G']