I want to iterate over elements from the end of a list to find the index of the last occurrence of an element which contains a '}'.
Ideally, I want to maintain efficiency using iterators rather than actually reversing the whole list or searching for all occurrences that match this condition.
If I were finding the first occurrence, I would use:
next(i for i,x in enumerate(list) if '}' in x)
However, I tried something similar using reversed to create a reverse iterator, but this did not seem to work, presumably because enumerate doesn't handle iterators, or maybe I have misunderstood...
next(i for i,x in enumerate(reversed(list)) if '}' in x)
I am aware other ways of doing this would be:
[i for i,x in enumerate(list) if '}' in x][-1]
or:
next(i for i,x in enumerate(list[::-1]) if '}' in x)
However, I am looking for the most efficient way. I could of course write a loop for efficiency, but was wondering if there was a neat way of doing it using the functions available.
I am relatively new to Python so go easy on me, and let me know if I have misunderstood anything. I am using Python 2.7.
Thanks in advance.