JPV
JPV

Reputation: 1079

Two list element wise comparison using filter

I have two lists of numbers:

l1 = [12, 3, 4, 5, 7]
l2 = [ 6, 8, 4, 2, 4] 

I want to retrieve all the elements from l1 that are bigger than the elements from l2 (element-wise comparison)

So far I only achieved

results = list(map(operator.gt,l1,l2)

Which returns me a [True,False,...] list. But I want the values itself.

I would like to do it without any for loop thanks. I was thinking about filter() or itertools()

Thanks

Upvotes: 1

Views: 561

Answers (1)

kederrac
kederrac

Reputation: 17322

you can use a list comprehension:

[a for a, b in zip(l1, l2) if a > b]

or you can use:

from operator import gt, itemgetter

list(map(itemgetter(1), filter(itemgetter(0), zip(map(gt, l1, l2), l1))))

Upvotes: 2

Related Questions