find index of values greater than python 1

find index of values greater than python

>>> [i for i,v in enumerate(a) if v > 4]
[4, 5, 6, 7, 8]
#enumerate returns the index and value of each item in an array. 
#So if the value v is greater than 4, include the index i in the new array.

Here is what the above code is Doing:
1. Create a new list
2. For each item in the array a, get the index and value
3. If the value is greater than 4, include the index in the new list

Similar Posts