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

Compare two lists elementwise with OR

I want to compare two lists of the same length by their elements with an OR.

>>> [0,0,1,1,0] or [1,1,0,1,0]
[0, 0, 1, 1, 0]

I want the outcome to be

[1,1,1,1,0]

How can I achieve the desired comparison?

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 :

You can use zip() function is used to iterate over corresponding elements of both lists simultaneously, and then a list comprehension is used to perform the OR operation on each pair of elements.

list1 = [0, 0, 1, 1, 0]
list2 = [1, 1, 0, 1, 0]

result = [a or b for a, b in zip(list1, list2)]
print(result)

Output:

[1, 1, 1, 1, 0]
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