need to get an index for an item in a list

Roy Smith roy at panix.com
Sun Jul 3 15:38:31 EDT 2005


In article <QfOdnSCv6tNToVXfRVn-hw at powergate.ca>,
 Peter Hansen <peter at engcorp.com> wrote:

> nephish at xit.net wrote:
> > i need to be able to get the index for an item in a list.
> 
> > any ideas?
> 
> Fire up the interactive interpreter and learn to use it to help 
> yourself.  In this case, the most useful thing might be to know about 
> the dir() builtin method, which you can use on a list like so:
> 
> dir([])
> 
> Note in the output the presence of the "index" method...  I'll leave it 
> up to you to read the docs to learn more about how to use it, or you can 
> just experiment at the prompt to see how it works.
> 
> -Peter

I certainly agree that dir() is a very handy tool to know about, and that 
poking around with it in the interactive interpreter is a great way to 
learn what's possible.

That being said, index() isn't isn't going to work if there are duplicate 
lines in the file; it'll return the index of the first one.



More information about the Python-list mailing list