Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comparing two lists in Python

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]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading