4

I like Python for it's expressiveness. I can't express everything as compact as I'd like to, though. For example this one I write quite often:

  def is_everything_okay(some_array):
    for s in some_array:
      if not is_okay(s):
        return False
    return True

But that's more Java then Python. How to improve the expressiveness (and probably execution speed) of that code snippet?

erikbstack
  • 12,878
  • 21
  • 81
  • 115
  • Are you asking about `any()` and `all()`? – S.Lott Feb 07 '12 at 22:21
  • Is any better in the case of an early false result (which doesn't need the checks to continue? How to formulate it correctly? – erikbstack Feb 07 '12 at 22:35
  • You have `timeit` to confirm the performance. If that's actually your question. – S.Lott Feb 07 '12 at 22:39
  • My question is about if you would like to write up a comprehensive answer to get my upvote and if better then Sven Marnach's also get my `accept`. – erikbstack Feb 07 '12 at 22:47
  • "My question is about "?? I still don't understand the question. I can't write up a comprehensive answer. Either it's simply about `any` and `all` or it's about the performance of `any` and `all`. Since `timeit` is available, you could easily post the timing so we can then answer a question. – S.Lott Feb 07 '12 at 23:18

2 Answers2

5

Use the built-in function all():

all(is_okay(s) for s in some_array)
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • looks much more like what I want. is it also faster or does it do the same "under the hood"? – erikbstack Feb 07 '12 at 22:26
  • 2
    @erikb: Of course it does the same -- what else should it do? It's slightly faster since it is written in C, but for this example, it still needs to call the Python function `is_okay()` for each `s`. – Sven Marnach Feb 07 '12 at 22:28
  • Doing the same in well written C is of course something different in terms of speed! Also it can be parallelised for faster results with multicore processors and/or order/index the array before the start if it helps anything. I have no idea! – erikbstack Feb 07 '12 at 22:31
  • 2
    It can't re-order the tests: the defined semantics are that it operates on the s's in order. – Ned Batchelder Feb 07 '12 at 22:33
  • Also does it include the speedup from returning False right when the first False is found? – erikbstack Feb 07 '12 at 22:35
  • 1
    Yes, both all() and any() short-circuit. – DSM Feb 07 '12 at 22:35
  • 1
    @erikb: Don't expect too much speed-up because the function is written in C. The test the must be executed in every iteration is still Python code. – Sven Marnach Feb 07 '12 at 22:41
0

Just to fill it in Sven's answer*... docs for 2.7 : http://docs.python.org/library/functions.html#all

all(iterable)
Return True if all elements of the iterable are true (or if the iterable is empty).

Equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
return True

Almost an exact copy of the code you show...

So, using a comprehension of the form (is_okay(s) for s in some_array) creates the iterable which is parsed by all()

Without specifically testing, you won't know which is faster.

  • and because I'm trying to wrap up a Python class I'm taking and need to answer some questions!
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
tom stratton
  • 668
  • 7
  • 13