[Tutor] Finding the Index of a member of a Tuple
Kent Johnson
kent37 at tds.net
Thu Jan 12 12:13:37 CET 2006
Steve Haley wrote:
> Hello everyone,
>
> I need to do something very simple but I'm having trouble finding the
> way to do it - at least easily. I have created a tuple and now need to
> find the position of individual members of that tuple. Specifically,
> the tuple is something like: words = ("you", "me", "us", "we", "and",
> "so", "forth") and I need to be able to name a member, for example, "us"
> and find what the position (index) of that word is in the tuple.
If you can use a list instead of a tuple you can use the index() method.
>>> words = ["you", "me", "us", "we", "and", "so", "forth"]
>>> words.index('me')
1
>>> words.index('so')
5
There is no index() method for a tuple. This is probably because in
GvR's view, tuples are analogous to records - they are heterogeneous
collections where position matters. Lists are for homogeneous
collections. In this view, tuple.index() doesn't make sense.
list.index() is documented here:
http://docs.python.org/lib/typesseq-mutable.html
You found the right chapter of the Lib Ref but the wrong section.
Kent
More information about the Tutor
mailing list