Fast way to do a boolean check between two lists in list comprehension in Python?

Viewed 241

Is anyone aware of a faster way to do this besides just splitting the list up and running in parallel?

I have 2 lists with T/F values. I am trying to calculate:

list3 = [False if l1[i] == False and l2[i] == False else True for i in range(len(l1))]

Thanks

2 Answers

Without using additional module:

This will produce same output as yours with less number of comparisons for each iteration(if lists has more True values):

list3 = [l1[i] or l2[i] for i in range(len(l1))]

Edit:

The answer suggested by Mandera seems faster than the above one here.

list3 = [a or b for a, b in zip(l1, l2)]

Thanks for suggesting this.

use numpy.logical_or():

import numpy
list3 = numpy.logical_or(list1, list2)
Related