how to detect non equal valus in 2 arrays

if i have 2 arrays a=[1,2,3,4,5] and b=[1,2,4,5,6] how to get a new array like this one: c=[1,2,0,0,0] basicly how to detect non equal values in python array and replace them on an other arraym appvalley

>>> [a[i] if a[i]==b[i] else 0 for i in range(len(a))]
[1, 2, 0, 0, 0]

c = [x if x == y else 0 for x, y in zip(a, b)]

1 Like

Also:
[x*(x == y) for x, y in zip(a, b)]
but it’s less readable than the one above.

This approach also assumes that the values can be multiplied - if the values are strings, then it produces an empty string instead of 0 for non-matching values, and if the values are some type that can’t be multiplied, it produces TypeError: unsupported operand type(s) for *

1 Like