Tuple index
Steven Bethard
steven.bethard at gmail.com
Mon Feb 21 16:31:32 EST 2005
Steve M wrote:
> I guess I explained my problem incorrectly. Let me try again.
>
> tuple = ("fred", "barney", "foo")
>
> I know that foo is an element of tuple, but what I need to know is what
> the index of foo is, tuple[?].
Larry Bates's solution is probably the best way to go here:
py> t = ("fred", "barney", "foo")
py> list(t).index("foo")
2
py> t[2]
'foo'
But note that if you're doing this often, you're probably using tuple
for the wrong things. Check out:
http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types
If you're iterating over the items of something and the items are all of
the same type, you probably want a list, not a tuple.
What's the use case in which you want to do this?
STeVe
More information about the Python-list
mailing list