[Tutor] Distinction between tuples and lists

Alan Gauld alan.gauld at btinternet.com
Thu Jan 1 19:59:23 CET 2009


"Christopher Mutel" <cmutel at gmail.com> wrote

> I stumbled across some discussion of why the fundamental difference
> between lists and tuples is not mutability, but hetero- versus
> homogeneous data, e.g.

This is not a discussion about Pythons tuples v lists per se but about
how some people think they should be used and their definitions/usage
within Computing science generally.

lists/tuples serve many purposes in Python but one thing they most 
definitely
do not do is act as hetero/homogenous containers. The authors of the 
blogs
may wish otherwise and indeed their debate seems to be geared around
what they would like to see in Python3000 rather than whats there now.

> http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/
> http://pyre.third-bit.com/blog/archives/000450.html
>
> However, after reading the cited discussions, my python books, etc., 
> I
> have to admit I don't really understand this idea. What does it mean
> that "lists are intended for homogeneous sequences"? What is 
> different
> about lists that would make them more suited for homogeneous 
> sequences

Nothing except the typical usage. Normally we use lists where
we want to be able to change their content or iterate over them.
The type of the data is often the same because the loop will try
to treat each item identically. Tuples are often used as "lightweight
records" so they hold fields of different type. Thus when accessing
a database we get back a list of tuples (records) with each tuple
containing the data firlds from the database.

But Python does not enforce that and a list of lists would work just
as well. And of course you can loop over a tuple as well as a list.

I wouldn't get overly hung up on these kinds of debates. I've never
seen any of the Python gurus make a big issue of it and experience
tends to teach where each type is more appropriate.

> from what I understand, the idea of homo/heterogeneity seems
> orthogonal to mutability, which is the main distinction emphasized 
> by
> e.g. Learning Python.

They are orthogonal concepts, you are quite right.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list