Re: list indexing

Discussion in 'Python' started by Sean Ross, Jul 31, 2003.

  1. Sean Ross

    Sean Ross Guest

    "Matthew" <> wrote in message
    news:...
    > Hello, I am rather new to python and I have come across a problem that
    > I can not find an answer to any where I my books. What I would like to
    > know is if there is a way to get the index number of a list element by
    > name. Sort of the inverse of the .index() method.


    Perhaps I've misunderstood, but it looks like you want:
    >>> words = ['this', 'is', 'a', 'list']
    >>> words.index('list')

    3

    which is, of course, not the inverse of .index() (but .index() itself).

    In general, if you use this, you'll either need to be certain that the item
    is indeed in the list, or nest the call in a try/except like so:
    >>> try:

    .... index = words.index('list')
    .... except ValueError:
    .... pass # or whatever you want to do when the look-up fails
    ....
    >>> index

    3

    HTH
    Sean
    Sean Ross, Jul 31, 2003
    #1
    1. Advertising

  2. Sean Ross

    Peter Abel Guest

    "Sean Ross" <> wrote in message news:<OGfWa.8894$>...
    > "Matthew" <> wrote in message
    > news:...
    > > Hello, I am rather new to python and I have come across a problem that
    > > I can not find an answer to any where I my books. What I would like to
    > > know is if there is a way to get the index number of a list element by
    > > name. Sort of the inverse of the .index() method.

    >
    > Perhaps I've misunderstood, but it looks like you want:
    > >>> words = ['this', 'is', 'a', 'list']
    > >>> words.index('list')

    > 3
    >
    > which is, of course, not the inverse of .index() (but .index() itself).
    >
    > In general, if you use this, you'll either need to be certain that the item
    > is indeed in the list, or nest the call in a try/except like so:
    > >>> try:

    > ... index = words.index('list')
    > ... except ValueError:
    > ... pass # or whatever you want to do when the look-up fails
    > ...
    > >>> index

    > 3
    >
    > HTH
    > Sean


    And if 'list' would appear more than once,
    the following could help:
    >>> words = ['this', 'is', 'a', 'list','and','this','is','a','list','too']
    >>> for i in range(len(words)):

    .... if words=='list':
    .... print i
    ....
    3
    8

    Regards,
    Peter
    Peter Abel, Aug 1, 2003
    #2
    1. Advertising

  3. Sean Ross

    Matthew Guest

    "Sean Ross" <> wrote in message news:<OGfWa.8894$>...
    > Perhaps I've misunderstood, but it looks like you want:
    > >>> words = ['this', 'is', 'a', 'list']
    > >>> words.index('list')

    > 3
    >
    > which is, of course, not the inverse of .index() (but .index() itself).
    >
    > In general, if you use this, you'll either need to be certain that the item
    > is indeed in the list, or nest the call in a try/except like so:
    > >>> try:

    > ... index = words.index('list')
    > ... except ValueError:
    > ... pass # or whatever you want to do when the look-up fails
    > ...
    > >>> index

    > 3
    >
    > HTH
    > Sean


    Christopher Koppler <> wrote in message news:<>...
    > Using a recent Python you can use enumerate (built-in):
    >
    > list = ['this', 'is', 'a', 'list']
    > for cnt, item in enumerate(list):
    > if item == 'list':
    > print cnt
    >
    >
    >
    > --Christopher


    Mark Day <> wrote in message news:<310720031327225125%>...
    >
    > If you're using Python 2.3, you can use enumerate() like this:
    >
    > for index,item in enumerate(list):
    > if item == 'list':
    > print index
    >
    > BTW: I'd suggest not using the name of a built-in class for your
    > variable "list". Perhaps "mylist" or "words"?
    >
    > -Mark




    Thanks Sean (& Mark and Christopher)
    You were right I misunderstoof the use of index(). It does do exactly
    what I want. However Mark and Christophers replies better suit this
    particular problem.

    Thanks very much All
    -matthew
    Matthew, Aug 1, 2003
    #3
    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. C
    Replies:
    0
    Views:
    496
  2. anony

    Drop down list indexing problem

    anony, Dec 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    anony
    Dec 22, 2003
  3. Emin
    Replies:
    4
    Views:
    408
    Paul McGuire
    Jan 12, 2007
  4. Skybuck Flying
    Replies:
    30
    Views:
    1,101
    Bill Reid
    Sep 19, 2011
  5. C
    Replies:
    3
    Views:
    219
    Manohar Kamath [MVP]
    Oct 17, 2003
Loading...

Share This Page