Tuple index
Steve M
steve at myplace.com
Mon Feb 21 16:56:55 EST 2005
Michael Hartl wrote:
> I actually find it strange that tuples don't have an index function,
> since finding the index doesn't involve any mutation. Anyone know why
> Python doesn't allow a statement like t.index('foo')?
>
> In any case, you can use the index method of list objects if you
> convert your tuple to a list first:
>
>>>> t = ("fred", "barney", "foo")
>>>> list(t).index("foo")
> 2
>>>> def index(a_tuple, element):
> ... return list(a_tuple).index(element)
> ...
>>>> t[index(t, "foo")]
> 'foo'
>
> (By the way, 'tuple' is a Python built-in type, so it's probably best
> to avoid using it as a variable name.)
>
>
> Michael
>
> --
> Michael D. Hartl, Ph.D.
> CTO, Quark Sports LLC
> http://quarksports.com/
The book I'm using to learn Python with has not gotten to lists yet, maybe
next chapter.
I knew tuple is a built in type, I was just trying to be clear, I guess I
just muddied the water a bit. Thank you for your help.
Steve
More information about the Python-list
mailing list