which lists python


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.
Who is Participating?
HonorGodConnect With a Mentor Commented:
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?
Don't use "list" as a variable name.

L = [3,4,5,2,4,3,5,6,3,2,1,0]
L.index( 5 )

Tells the location of the list variable that contains the specified value.

Is this what you want?
dfernanAuthor Commented:
No. I want to check for a condition. L.which(>=5) ... index in the list where the values are bigger or equal to 5. In your case, [2,6,7] cause in those positions L is bigger or equal to 5.
dfernanAuthor Commented:
sorry now i read ur second answer! yes, that's what i want. thanks!
Great.  Glad to be of some help.

Thanks for the grade & points.

Good luck & have a great day.
All Courses

From novice to tech pro — start learning today.