-1

Is there a way in python, to compare a value in one list with the value of the same index in a second list?

For example: I want to see if the value in list1[0] > list2[0] and list1[1] > list2[1] and do that for the entire list, but return only the values that are greater in list1.

(Note I only want to compare a number in list1 that directly aligns with the same index in list2. so list1[0] is only tested against list2[0] and not list2[1] or list2[2] etc.)

list1 = [100, 223, 335, 143, 218]
list2 = [75, 245, 400, 86, 500]
smci
  • 32,567
  • 20
  • 113
  • 146
PyPro
  • 41
  • 2
  • 6
  • 2
    At the very least you could do this with a basic loop. Did you try that? – sco1 Apr 06 '18 at 20:19
  • Yes, you mean ***"compare lists pairwise"***. Are they guaranteed to have the same length? Do you want it in native Python, or pandas, or both? – smci Apr 06 '18 at 20:20
  • If you're looking for a pythonic solution, read about the [`zip()`](https://stackoverflow.com/questions/13704860/zip-lists-in-python) function. – pault Apr 06 '18 at 20:22

2 Answers2

3

If you wouldn't mind using numpy(), you can just use advanced indexing to get the elements in list1 greater than their respective value in list2:

In [7]: list1 = np.array([100, 223, 335, 143, 218])
   ...: list2 = np.array([75, 245, 400, 86, 500])
   ...:

In [8]: list1[list1 > list2]
Out[8]: array([100, 143])

If you end up wanting to do this calculation on very large lists, using a library such as numpy() will increase your performance quite a bit:

In [5]: a = np.random.rand(10000)

In [6]: b = np.random.rand(10000)

In [7]: %timeit a[a > b]
56.5 µs ± 1.3 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [8]: %timeit [i for i, j in zip(a, b) if i >j]
1.83 ms ± 28.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
user3483203
  • 50,081
  • 9
  • 65
  • 94
1

You can zip the two lists togather, and compare the values:

>>> list1 = [100, 223, 335, 143, 218]
>>> list2 = [75, 245, 400, 86, 500]
>>> 
>>> for i, j in zip(list1, list2): print i, j, i < j
... 
100 75 False
223 245 True
335 400 True
143 86 False
218 500 True

To filter the larger values from list1, use list comprehension:

>>> [i for i, j in zip(list1, list2) if i >j]
[100, 143]
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • The output the OP wants is "only the values that are greater in `list1`" – smci Apr 06 '18 at 20:24
  • @smci Thanks for pointing that out, have added in the answer. – Anshul Goyal Apr 06 '18 at 20:28
  • How do you get a list comprehension version but that only yields the items from list1? i.e. compress the `None` out of `[i1 if (i1>i2) else None for i1,i2 in zip(list1, list2)] = [100, None, None, 143, None]` – smci Apr 06 '18 at 20:35
  • @smci I didn't understand you.. Are you suggesting that None is needed in the final result for cases where `i <= j`? – Anshul Goyal Apr 06 '18 at 20:39
  • No @mu-無, I'm asking the total opposite: how to *remove* it from the list-comprehension version, so you get your desired result. – smci Apr 06 '18 at 21:12