Python etc / `enumerate`

enumerate

In Python, for lets you withdraw elements from a collection without thinking about their indexes:

def find_odd(lst):
    for x in lst:
        if x % 2 == 1:
            return x

    return None

If you do care about indexes you can iterate over range(len(lst)):

def find_odd(lst):
    for i in range(len(lst)):
        x = lst[i]
        if x % 2 == 1:
            return i, x

    return None, None

But perhaps the more semantically correct and expressive way to do the same it to use enumerate:

def find_odd(lst):
    for i, x in enumerate(lst):
        if x % 2 == 1:
            return i, x

    return None, None