[Solved] Finding different previous value in a list in python?


One approach using extended iterable unpacking and next:

lst = [1, 7, 9, 8, 5, 5, 5, 5, 5, 5, 5, 5, 5]

last, *front = reversed(lst)
res = next((v for v in front if last != v), None)
print(res)

Output

8

The above approach is equivalent to the following for-loop:

last, *front = reversed(lst)

res = None
for v in front:
    if last != v:
        res = v
        break

print(res)

solved Finding different previous value in a list in python?