Velocity Reviews > Tuple question

# Tuple question

Will McGugan
Guest
Posts: n/a

 09-02-2004
Hi,

Why is that a tuple doesnt have the methods 'count' and 'index'? It
seems they could be present on a immutable object.

I realise its easy enough to convert the tuple to a list and do this,
I'm just curious why it is neccesary..

Thanks,

Will McGugan

Wai Yip Tung
Guest
Posts: n/a

 09-02-2004
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
>>>

> Hi,
>
> Why is that a tuple doesnt have the methods 'count' and 'index'? It
> seems they could be present on a immutable object.
>
> I realise its easy enough to convert the tuple to a list and do this,
> I'm just curious why it is neccesary..
>
>
> Thanks,
>
> Will McGugan

Will McGugan
Guest
Posts: n/a

 09-02-2004
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'

Wai Yip Tung
Guest
Posts: n/a

 09-02-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
<(E-Mail Removed)> 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'

Gandalf
Guest
Posts: n/a

 09-02-2004

> 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.

Why?

A list can hold any kind of objects. I think that the 'index' method for
tuples would be a good idea.

Peter Hansen
Guest
Posts: n/a

 09-02-2004
Will McGugan wrote:
> Why is that a tuple doesnt have the methods 'count' and 'index'? It
> seems they could be present on a immutable object.
>
> I realise its easy enough to convert the tuple to a list and do this,
> I'm just curious why it is neccesary..

and

-Peter

Roy Smith
Guest
Posts: n/a

 09-02-2004
In article <(E-Mail Removed)>,
Gandalf <(E-Mail Removed)> wrote:

> > 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.

>
> Why?
>
> address = ['www.python.org',80]
>
> A list can hold any kind of objects. I think that the 'index' method for
> tuples would be a good idea.

Personally, I think it should be more general. I think index should be
a sequence method, and tuple should just inherit from that.

Donn Cave
Guest
Posts: n/a

 09-02-2004
In article <(E-Mail Removed)>,
Gandalf <(E-Mail Removed)> wrote:

> > 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.

>
> Why?
>
> address = ['www.python.org',80]
>
> A list can hold any kind of objects. I think that the 'index' method for
> tuples would be a good idea.

Yes, lists and tuples can hold the same kinds of objects.
I don't care whether I manage to convince you that the
index method is not needed, but here's my take on an aspect
of the the homogeneity issue, a somewhat obscure point that
isn't explained in the FAQ.
(
http://www.python.org/doc/faq/genera...parate-tuple-a
nd-list-data-types )

Lists are not naturally homogeneous because each item is
of the same type as the next. That would be sort of absurd
in a language like Python, where that kind of typing isn't
done. Rather they are homogeneous because if you say that
that an object is "list of (something)", typically a slice
of that list will still be a valid "list of (something)" -
a list of hosts, a list of dictionary keys, etc. In this
less concrete sense of type, the list itself has a type
that applies not only to the whole list but to any slice.
The list object has all kinds of support for iterative
traversal, deletion, extension, etc., because these are
naturally useful for this kind of sequence.

On the other hand, we normally use tuples for data that
is meaningful only when it's intact. The (key, value)
pair that comes back from dict.items(), for example. Each
value may very well be a string, but the sequence is not
homogeneous in the sense we're talking about, and index()
is not useful.

Donn Cave, http://www.velocityreviews.com/forums/(E-Mail Removed)

Colin J. Williams
Guest
Posts: n/a

 09-02-2004

Wai Yip Tung wrote:
> 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.

print users

Colin W.
> Therefore they are not defined for tuple.
>
> tung
>
> On Thu, 02 Sep 2004 17:40:27 +0100, Will McGugan
> <(E-Mail Removed)> 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'

>
>

Gandalf
Guest
Posts: n/a

 09-02-2004

> Please see these recent threads, and read the FAQ:
>

This is from that thread:

>Note that while .index() makes sense for some sequences,
>such as strings and lists, it doesn't make sense for the
>way in which tuples are "supposed to be used", which is
>as collections of heterogeneous data and not usually as

Why it is not useful to have an index() method for collections of heterogeneous data?

Suppose, you have big amount of data stored in tuples (for using less memory).
You may want to extract slices from the tuploes from a given index determined by an object.
This is just an example, however it is quite realistic (e.g. using tuples instead of lists
because there is a huge amount of static data that you need to access quickly).