In [1]:
# functions taken from this http://stackoverflow.com/a/16801638

from itertools import ifilter, islice

def OP(l):
    true_found = False
    for v in l:
        if v and not true_found:
            true_found=True
        elif v and true_found:
             return False #"Too Many Trues"
    return true_found

def DavidRobinson(l):
    return l.count(True) == 1

def FJ(l):
    return len(list(islice(ifilter(None, l), 2))) == 1

def JonClements(iterable):
    i = iter(iterable)
    return any(i) and not any(i)

def moooeeeep(l):
    true_found = False
    for v in l:
        if v:
            if true_found:
                # found too many True's
                return False 
            else:
                # found the first True
                true_found = v
    # found zero or one True value
    return true_found


def facubatista_one(iterable):
    iterable = iter(iterable)
    for item in iterable:
        if item:
            break
    else:
        return False
    if any(iterable):
        return False
    return item


from one import one

In [2]:
l1 = [0, 0, 0, 'hi', 0, 0, 0]

In [3]:
# return boolean 
%timeit OP(l1)
%timeit DavidRobinson(l1)
%timeit FJ(l1)
%timeit JonClements(l1)


1000000 loops, best of 3: 568 ns per loop
1000000 loops, best of 3: 334 ns per loop
1000000 loops, best of 3: 1.1 µs per loop
1000000 loops, best of 3: 321 ns per loop

In [4]:
# return the item
%timeit moooeeeep(l1)
%timeit facubatista_one(l1)
%timeit one(l1)  # the current one implementation


1000000 loops, best of 3: 346 ns per loop
1000000 loops, best of 3: 378 ns per loop
1000000 loops, best of 3: 452 ns per loop

In [7]:
l2 = [False for i in xrange(100000)] + [True]

In [8]:
# return the item
%timeit moooeeeep(l2)
%timeit facubatista_one(l2)
%timeit one(l2)  # the current one implementation


1000 loops, best of 3: 1.44 ms per loop
1000 loops, best of 3: 1.44 ms per loop
100 loops, best of 3: 2.5 ms per loop

In [ ]: