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?
>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]