Newbie question on tuples

Skip Montanaro skip at pobox.com
Thu Feb 5 16:17:19 EST 2004


    Thomas> How does one find the position of an item in a tuple. i.e. If
    Thomas> the tuple is ("rock", "scissors", "paper"), what function (or
    Thomas> method) do I call with "rock" to get 0 or with "paper" to get 2?

Tuples don't have an index method, but lists do, so you can list-ify your
tuple:

    >>> list(("rock", "scissors", "paper")).index("rock")
    0
    >>> list(("rock", "scissors", "paper")).index("paper")
    2

Skip




More information about the Python-list mailing list