Question : which lists python

Hi,

Very short question. How do you search (without using for loops) in which position of an array certain condition holds.

I.e., list = [3,4,5,2,4,3,5,6,3,2,1,0]
which(list < 2) = [10, 11] cause in positions 10 and 11 the elements of the list are less than 2.

Answer : which lists python

List comprehensions kind of work, so

>>> L = [3,4,5,2,4,3,5,6,3,2,1,0]
>>> [ i for i in L if i < 2 ]
[1, 0]

But that gives us the values, not the index.  Combining them

>>> [ L.index( i ) for i in L if i < 2 ]
[10, 11]

appears to work.  Is that what you want?
Random Solutions  
 
programming4us programming4us