8

Suppose I have two lists (or numpy.arrays):

a = [1,2,3]
b = [4,5,6]

How can I check if each element of a is smaller than corresponding element of b at the same index? (I am assuming indices are starting from 0) i.e.

at index 0 value of a = 1 < value of b = 4
at index 1 value of a = 2 < value of b = 5
at index 2 value of a = 3 < value of b = 6

If a were equal to [1,2,7], then that would be incorrect because at index 2 value of a is greater than that of b. Also if a's length were any smaller than that of b, it should be comparing only the indices of a with those of b.

For example this pair a, b

a = [1,2]
b = [3,4,5]

at indices 0 and 1, value of a is smaller than b, thus this would also pass the check.

P.S.--> I have to use the above conditions inside a if statement. And also, no element of a should be equal to that of b i.e. strictly lesser. Feel free to use as many as tools as you like. (Although I am using lists here, you can convert the above lists into numpy arrays too.)

4 Answers 4

14

Answering both parts with zip and all

all(i < j for (i, j) in zip(a, b))

zip will pair the values from the beginning of a with values from beginning of b; the iteration ends when the shorter iterable has run out. all returns True if and only if all items in a given are true in boolean context. Also, when any item fails, False will be returned early.

Example results:

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> all(i < j for (i, j) in zip(a, b))
True
>>> a = [1,2,7]
>>> b = [4,5,6]
>>> all(i < j for (i, j) in zip(a, b))
False
>>> a = [1,2]
>>> b = [4,5,-10]
>>> all(i < j for (i, j) in zip(a, b))
True

Timings with IPython 3.4.2:

In [1]: a = [1] * 10000
In [2]: b = [1] * 10000
In [3]: %timeit all(i < j for (i, j) in zip(a, b))
1000 loops, best of 3: 995 µs per loop
In [4]: %timeit all(starmap(lt, zip(a, b)))
1000 loops, best of 3: 487 µs per loop

So the starmap is faster in this case. In general 2 things are relatively slow in Python: function calls and global name lookups. The starmap of Retard's solution seems to win here exactly because the tuple yielded from zip can be fed as-is as the *args to the lt builtin function, whereas my code needs to deconstruct it.

5

As a variant, fast and short

from operator import lt
from itertools import starmap, izip
all(starmap(lt, izip(a, b)))
8
  • @Antti Haapala, for the a and b, where a = list(range(10000)) and b = list(range(1, 10001)) On my system your solution gives 809 microseconds and mine is 558 Feb 27, 2015 at 14:54
  • Indeed yours is the faster one :D because of less tuple creation. Feb 27, 2015 at 14:58
  • 1
    Is there any reason you didn't use itertools.izip?
    – wwii
    Feb 27, 2015 at 15:19
  • 1
    On my system izip is drastically faster Feb 27, 2015 at 15:26
  • 3
    Hey, we're talking about Python 3. zip is itertools.izip from Python 2.
    – Matthias
    Feb 27, 2015 at 18:18
2

Since this question has a numpy tag, I figured I would provide a numpy solution.

You can only use a < operator on arrays of equal length, therefore if your arrays are of different length you need to shorten the longest one.

In [26]: import numpy as np

In [27]: a = [1,2,3]

In [28]: b = [4,5,6]

In [29]: np.all(a < b)
Out[29]: True

In [30]: a = [1,2]

In [31]: b = [3,4,5]

In [32]: shortest = min(len(a), len(b))

In [33]: np.all(a[:shortest] < b[:shortest])
Out[33]: True
1

Just an alternative way, and not sure how it performs against the other answers:

a = [1,2,3]
b = [4,5,6]
if filter(lambda x: x[0] < x[1], zip(a,b)):
    return True

I'm using it within the if-statement since the question indicates, that it's going to be used like that, and there's no need for a bool-conversion there. Otherwise I would've wrapped the filter() inside bool(). Again, this is just to demonstrate an alternative approach, not meaning to be the most efficient one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.