Member index in toples

Tim Chase python.list at tim.thechases.com
Thu Jun 1 15:36:07 EDT 2006


> I have a tuple like this:
> 
> T = ("One","Two","Three","Four")
> 
> Is there any built-in way to find what is the index of "Two" withouot 
> looping within the tuple?
> 
> Is the same feature available for lists or dictionaries?

Lists have a index() method.  For the tuple, you can convert it 
to a list:

	indexOfTwo = list(T).index("Two")

Dictionaries don't have a defined ordering, so there's no similar 
functionality (because results would be meaningless) unless you 
convert it to a list, and then you can snag its index in the 
arbitrarily-ordered results (or sort the results first).

If you just need to test for presence, and don't need the index, 
you can use

	"Two" in T

Just a few ideas...

-tkc






More information about the Python-list mailing list