Hi Andrea,

[EMAIL PROTECTED] wrote:
      I was wondering if there is a faster/nicer method (than a for loop)
that will allow me to find the elements (AND their indices) in a list that
verify a certain condition. For example, assuming that I have a list like:

mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]

I would like to find the indices of the elements in the list that are equal
to 1 (in this case, the 1,2,3,4,9 elements are equal to 1). I could easily
use a for loop but I was wondering if there is a faster method...

you could use something like this:

>>> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
>>> [x[0] for x in enumerate(mylist) if x[1] == 1]
[1, 2, 3, 4, 9]

Greetings,
  Joachim

--
Joachim Bauch, struktur AG
Download icoya OpenContent 1.3 for FREE!    visit http://www.icoya.de/iOC4free 
<-

Attachment: signature.asc
Description: OpenPGP digital signature

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to