Tuple question

Wai Yip Tung tungwaiyip at yahoo.com
Thu Sep 2 12:52:58 EDT 2004


Oops I misunderstood that you said about count and index. Now I got it.

Speaking as a user of Python, here is my take:

You consider tuple an immutable version of list. But in Python's design  
they have different purpose. List a collection of homogeneous items, while  
tuple is  a convenient grouping of any kind of items. For example, you can  
use them this way:

users = ['admin', 'user1', 'user2']
address = ('www.python.org', 80)

index and count only make sense when the collection is homogeneous.  
Therefore they are not defined for tuple.

tung

On Thu, 02 Sep 2004 17:40:27 +0100, Will McGugan  
<news at NOwillmcguganSPAM.com> wrote:

> Wai Yip Tung wrote:
>
>> I'm not sure what do you mean by index. But you can use len() to get  
>> the  number of objects in a tuple. e.g.
>>
>>>>> t=(1,2,3)
>>>>> len(t)
>>  3
>>
>>>>> t[2]
>>  3
>>
>
> Lista have an index method that returns the index of the first occurance  
> of an element, but tuple doesnt (nor count). Just wondering why.
>
>  >>> l= [ 1, 2, 3 ]
>  >>> t= ( 1, 2, 3 )
>  >>> l.index(2)
> 1
>  >>> t.index(2)
> Traceback (most recent call last):
>    File "<pyshell#8>", line 1, in ?
>      t.index(2)
> AttributeError: 'tuple' object has no attribute 'index'




More information about the Python-list mailing list