1

I'd like to do something like this:

x = f(a[0]) or f(a[1]) or f(a[2]) or f(a[3]) or …

with a given list a and a given function f. Unlike the built-in any function I need to get the first value of the list which is considered to be true; so for 0 or "foo" or 3.2 I need to get "foo", not just True.

Of course, I could write a small function like

def returnFirst(f, a):
  for i in a:
    v = f(i)
    if v:
      return v
  return False

x = returnFirst(f, a)

but that's probably not the nicest solution, for reasons also given in this SO question. As I mention this other thread, I could of course use code based on the solution given there, e.g.

x = next((f(x) for x in a if f(x)), False)

But I don't see a simple way to circumvent the doubled calling of f then.

Is there any simple solution I am missing or just don't know? Something like an

OR((f(x) for x in a))

maybe?

I tried to find other questions concerning this, but searching for keywords like or is a bit problematic in SO, so maybe I just didn't find something appropriate.

Community
  • 1
  • 1
Alfe
  • 56,346
  • 20
  • 107
  • 159
  • 1
    I don't see anything wrong with `returnFirst`. It's encapsulated, in one place. Not creating a utility function that you need that doesn't exist because people won't use it is trying to optimize someone else's workflow. I disagree that it's tasteless. A small dose of documentation can fix the problem entirely. – Chris Pfohl Aug 13 '13 at 12:19
  • 5
    duplicate:http://stackoverflow.com/questions/1077307/why-is-there-no-firstiterable-built-in-function-in-python – Bas Swinckels Aug 13 '13 at 16:03

2 Answers2

2

This should work:

next((x for y in a for x in (f(y),) if x),False)
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • Ah, beat me to the punch. Does next return `False` if nothing's there, or `None`? Might want to add the default value (`s/if x/if x, False/`) – Chris Pfohl Aug 13 '13 at 12:25
  • Yeah, I don't really like those abuses of generators for having "local variables" in apprehensions etc. But it works, right. Upvote. – Alfe Aug 13 '13 at 13:36
  • @Alfe: Agreed, I think using the separate function is clearer. – Vaughn Cato Aug 13 '13 at 13:39
0

I'm now using this:

x = next((v for v in (f(x) for x in a) if v), False)

It's a working idiom without doubling the call of f and without introducing a hacky local, but it still is not very readable (especially if x, f, a, and v are longer than one letter).

I'd be happy to hear of a better solution.

Alfe
  • 56,346
  • 20
  • 107
  • 159