473,837 Members | 1,888 Online

# Find Items & Indices In A List...

Hello NG,

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...

Thanks for every suggestion.

Andrea.
------------------------------------------------------------------------------------------------------------------------------------------
Message for the recipient only, if received in error, please notify the
Jul 18 '05 #1
5 2136
<an***********@ agip.it> a écrit dans le message de news:
ma************* *************** **...*@pyth on.org...
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).

PythonWin 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
win32.
Portions Copyright 1994-2004 Mark Hammond (mh******@skipp inet.com.au) - see
mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
[i for i, j in enumerate(mylis t) if j==1] [1, 2, 3, 4, 9]

Jul 18 '05 #2
an***********@a gip.it wrote:
Hello NG,

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...

Thanks for every suggestion.

Andrea.
------------------------------------------------------------------------------------------------------------------------------------------
Message for the recipient only, if received in error, please notify the

You could do a list comprehension /generator expression. Like this:
[i for i in range(len(mylis t)) if mylist[i] == 1]

--
--------------------------------------
Ola Natvig <ol********@inf osense.no>
infoSense AS / development
Jul 18 '05 #3
On Fri, 10 Dec 2004 16:01:26 +0100, an***********@a gip.it wrote:
Hello NG,

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...

Thanks for every suggestion.

One way:
mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
[i for i,v in enumerate(mylis t) if v==1]

[1, 2, 3, 4, 9]

Regards,
Bengt Richter
Jul 18 '05 #4
an***********@a gip.it wrote:
Hello NG,

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...

Everyone has already given you the answer (enumerate in a LC or GE), I'd
just comment that it's easy enough to extend their answers to any given
condition:
def getindices(sequ ence, predicate): .... return [i for i, v in enumerate(seque nce) if predicate(v)]
.... getindices([0,1,1,1,1,5,6,7 ,8,1,10], bool) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def equalsone(v): .... return v == 1
.... getindices([0,1,1,1,1,5,6,7 ,8,1,10], equalsone) [1, 2, 3, 4, 9] def f(v): .... return pow(v, 3, 4) == 3
.... getindices([0,1,1,1,1,5,6,7 ,8,1,10], f)

[7]

Steve
Jul 18 '05 #5
On Fri, 10 Dec 2004 16:27:29 GMT, Steven Bethard <st************ @gmail.com> wrote:
an***********@ agip.it wrote:
Hello NG,

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...

Everyone has already given you the answer (enumerate in a LC or GE), I'd
just comment that it's easy enough to extend their answers to any given
condition:
def getindices(sequ ence, predicate):... return [i for i, v in enumerate(seque nce) if predicate(v)]
... getindices([0,1,1,1,1,5,6,7 ,8,1,10], bool)[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] def equalsone(v):... return v == 1
... getindices([0,1,1,1,1,5,6,7 ,8,1,10], equalsone)[1, 2, 3, 4, 9] def f(v):... return pow(v, 3, 4) == 3
... getindices([0,1,1,1,1,5,6,7 ,8,1,10], f)

[7]

Conclusion:
Python is programmer's Lego ;-)

Regards,
Bengt Richter
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.