Using next with a generator expression:
lst = [1,2,3,4,5,6,7,8,9,8,7,6,5,4,3,2,1]
res = next(j for i, j, k in zip(lst, lst[1:], lst[2:]) if i < j and i == k)
If you need all such numbers, use a list comprehension instead:
res = [j for i, j, k in zip(lst, lst[1:], lst[2:]) if i < j and i == k]
If you need a condition that will show all numbers that are higher than their previous and next ones:
lst = [1,2,3,4,3,2,3,1,2,1,2,3,4,5,6,7,8,6]
res = [j for i, j, k in zip(lst, lst[1:], lst[2:]) if i < j > k]
[4, 3, 2, 8] is printed.
Explanation
- You can iterate the list with shifted versions of itself via
zip.
- For each triplet, test your two conditions.
- Use
next to extract such triplets; if no such triplet exists, you will meet StopIteration error.
- Never name a variable after a built-in, e.g. use
lst instead of list.