Python: Get First Occurrence of Value in Array

(Last Updated On: )

If you are searching for the first value in an array. There are a few ways to do this. I will show you the easiest to me.

You can do this with the following. It will return 34 as the result.

val_list = [0,0,34,21,0,657,345,32,34,5,8,9]

result = next(x for x in val_list if x > 0)
print(result)