Skip to content Skip to sidebar Skip to footer

Compare Corresponding Elements Of A List

I am trying to perform a simple comparison between 2 lists. How do I compare one elements from list A to the corresponding element of list B? Lets say I have 2 lists. A = [100,100,

Solution 1:

Function zip will generate for you pairs of elements:

>>> print(list(zip(A, B)))
[(100, 100), (100, 120), (100, 95)]

Now you can run an easy pairwise comparison using a list comprehension:

>>> [a > b for (a, b) inzip(A, B)]
[False, False, True]

Now you easily can check whether the comparison holds for every element:

>>> all(a > b for (a, b) inzip(A, B))
False

I hope this helps.

Post a Comment for "Compare Corresponding Elements Of A List"