Tuple question

Donn Cave donn at u.washington.edu
Thu Sep 2 20:27:41 CEST 2004

In article <mailman.2775.1094145196.5135.python-list at python.org>,
 Gandalf <gandalf at geochemsource.com> 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.
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, donn at u.washington.edu

More information about the Python-list mailing list