The reason why you get (1,1,0)
is due to short-circuting, and also due to your code. Let me break it down for you.
First iteration:
num == []
element == (1,1,0)
num or element == (1,1,0)
num == (1,1,0) # due to assignment to num
Second iteration:
num == (1,1,0) # value from previous iteration
element == (0,0,1) # 2nd value of row
num or element == (1,1,0) # due to Short circuiting
num == (1,1,0) # due to "assignment" to num, in contrast to appending
So, num
ends up with (1,1,0)
.
Solution
You can do what Avinash Raj showed. Here is another way to do it:
row = [(1,1,0),(0,0,1)]
result = tuple(row[0][i] or row[1][i] for i in range(3))
print(result)
Output:
(1, 1, 1)