I came across a question were someone wanted to generate list3 = [1, 0, 1, 0, 0]
for list1 = [0, 1, 0, 0, 1
] and list2 = [0, 1, 0, 1, 1]
.
If one would zip(list1, list2)
therefore only zip(0, 0)
would generate 1
for list3
.
I want to express the subsequent code by a list comprehension:
L3 = []
for x in zip(L1, L2):
if sum(x) == 0:
L3.append(1)
else:
L3.append(0)
print L3
I can get the same output by using this list comprehension:
print [int(sum(x)==0) for x in zip(L1, L2)]
I would like to know if there is a way to express the if/else condition of the initial code block in a list comprehension more literally. I can understand this question might be considered superfluous as the if/else condition naturally is comprised, but alternatives posted could be very useful to me.