Q:

Find previous matching element in a cyclic list

I have a list of True and False, which is considered cyclic (first element comes after the last one). What is the better way to find index of the closest True element, preceding an element with the given index?
Here is my solution. It is very straightforward, but not very pythonic, as I think:
def prev_element(a, i):
    i -= 1
    while not a[i]:
        i -= 1
    return i % len(a)

This will raise IndexError in case there is no such element, which is ok for me. I exploit the fact that a list may be indexed with negative indices, which makes the list cycling. Any better solutions? Some cool one-liner, maybe?

A:

from itertools import  count

def prev_element(a, i):
    cyc = count(i - 1, -1)
    res = next((next(cyc) + 1 for _ in range(len(a)) if a[next(cyc)]), None)
    return res % len(a) if res else res

You can use max with a try/except using a lambda to get the max, under the constraint that it cannot be less than the index we are searching from, if we have no index below it will get the index above.
def prev_element(a, ind):
    try:
        return max((i for i, ele in enumerate(a) if ele and i != ind), key=lambda x: (x < ind, x))
    except ValueError:
        return None

Without a try/except use a list:
def prev_element(a, ind):
    inds  = [i for i, ele in enumerate(a) if ele and i != ind]
    return max(inds,key=lambda x: (x < ind, x)) if inds else None

