Comparing two lists in Python

Advertisements

I have two lists sol1 and cond1. I want to compare these two lists and print values from sol1 corresponding to False in cond1. For instance, False occurs at cond1[0],cond1[2] and the corresponding values are sol1[0],sol1[2]. I present the expected output.

sol1 = [1, 2, 4, 6, 7]
cond1 = [False, True, False, True, True]

The expected output is

[1,4]

>Solution :

By using zip you can iterate over pairs of items, and use a list comprehension to filter out True values:

sol1 = [1, 2, 4, 6, 7]
cond1 = [False, True, False, True, True]

result = [value for value, condition in zip(sol1, cond1) if not condition]
print(result)
>>> [1, 4]

Leave a Reply Cancel reply