Answer both parts with zip and all
all(i < j for (i, j) in zip(a, b))
zip associates the values ββfrom the beginning of a with the values ββfrom the beginning of b ; the iteration ends when the shorter iterative has ended. all returns True if and only if all elements in the given state are true in a boolean context. In addition, when any item fails, False will be returned earlier.
Examples of 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
Timing 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
Thus, in this case, starmap is faster. In general, in Python 2, things are relatively slow: function calls and name queries. starmap Retard solution seems to win here precisely because the tuple obtained from zip can be served as something like * args to the built-in lt , while my code should deconstruct it.