python index function

January 8, 2013

A cool, little Python index function I came across recently.  It’s simple and compact without being obscure; all good things.

def index(seq, f):
    """Return the index of the first item in seq where f(item) == True."""
    return next((i for i,v in enumerate(seq)) if f(v), None)

Leave a Reply

Your email address will not be published. Required fields are marked *


*