Find Items & Indices In A List...

Discussion in 'Python' started by andrea.gavana@agip.it, Dec 10, 2004.

  1. Guest

    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
    sender and read http://www.eni.it/disclaimer/
    , Dec 10, 2004
    #1
    1. Advertising

  2. kaerbuhez Guest

    <> a écrit dans le message de news:
    ...
    > 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).


    List comprehension is your friend:

    PythonWin 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on
    win32.
    Portions Copyright 1994-2004 Mark Hammond () - see
    'Help/About PythonWin' for further copyright information.
    >>> mylist = [0, 1, 1, 1, 1, 5, 6, 7, 8, 1, 10]
    >>> [i for i, j in enumerate(mylist) if j==1]

    [1, 2, 3, 4, 9]
    >>>
    kaerbuhez, Dec 10, 2004
    #2
    1. Advertising

  3. Ola Natvig Guest

    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
    > sender and read http://www.eni.it/disclaimer/
    >
    >


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

    --
    --------------------------------------
    Ola Natvig <>
    infoSense AS / development
    Ola Natvig, Dec 10, 2004
    #3
  4. On Fri, 10 Dec 2004 16:01:26 +0100, 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(mylist) if v==1]

    [1, 2, 3, 4, 9]

    Regards,
    Bengt Richter
    Bengt Richter, Dec 10, 2004
    #4
  5. 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(sequence, predicate):

    .... return [i for i, v in enumerate(sequence) 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
    Steven Bethard, Dec 10, 2004
    #5
  6. On Fri, 10 Dec 2004 16:27:29 GMT, Steven Bethard <> wrote:

    > 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(sequence, predicate):

    >... return [i for i, v in enumerate(sequence) 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
    Bengt Richter, Dec 10, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SnuSnu
    Replies:
    6
    Views:
    334
    Steven Rumbalski
    Apr 25, 2004
  2. Girish Sahani
    Replies:
    7
    Views:
    5,475
    Larry Bates
    Jun 13, 2006
  3. Replies:
    3
    Views:
    386
    Gerard Flanagan
    Jun 30, 2006
  4. ÃCø¯
    Replies:
    4
    Views:
    677
    ÃCø¯
    Jan 22, 2008
  5. blumenkraft

    delete items from list by indices

    blumenkraft, Sep 23, 2009, in forum: Python
    Replies:
    4
    Views:
    318
    blumenkraft
    Sep 23, 2009
Loading...

Share This Page