() vs []

Tim Golden mail at timgolden.me.uk
Thu Oct 15 10:55:24 CEST 2009


Xavier Ho wrote:
> On Thu, Oct 15, 2009 at 6:39 PM, Chris Rebert <clp2 at rebertia.com> wrote:
> 
>> Nanjundi meant "index method" as in "a method .index()" (i.e. a method
>> named "index") which searches through the container for the given item
>> and returns the index of the first instance of said item, like
>> list.index() does.
>>
>> Interesting interpretation.. but I just gave it a try.
> 
>>>> a = (1,2,3,4)
>>>> a
> (1, 2, 3, 4)
>>>> a.index(3)
> 2
>>>> a.index(5)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ValueError: tuple.index(x): x not in tuple
> 
> So my Python is saying that tuples do implement .index() method. What gives?

It was added relatively recently, around Python 2.6 I think,
at least partly as an oh-ok-then reaction to everyone asking:
"how come lists have .index and .count and tuples don't?"
and neverending "tuples-are-immutable-lists-no-they-aren't-yes-they-are"
debate.

TJG



More information about the Python-list mailing list