Tuples vs Lists: Semantic difference (was: Extract String From Enclosing Tuple)

Ben Finney bignose+hates-spam at benfinney.id.au
Thu Mar 1 04:45:01 CET 2007


Bjoern Schliessmann <usenet-mail-0306.20.chr0n0ss at spamgourmet.com> writes:

> Ben Finney wrote:
>
> > A tuple implies a meaning associated with each position in the
> > sequence (like a record with a positional meaning for each field),
> > a list implies the opposite (a sequence with order but not meaning
> > associated with each position).
>
> Explain.

Well, since you ask so politely :-)

> I know tuples as immutable lists ...

That's a common misconception.

Tuples are intended for use as heterogeneous data structures: every
index in the sequence *means* something, a semantic meaning applied to
the item at that index. It's for this reason that a tuple is
immutable: removing items, inserting them in the middle, etc. would
imply that the index doesn't have semantic meaning for the structure,
which is not true.

Lists are intended for use as homogeneous sequences: not that every
value is of the same type, but that a particular index in the sequence
doesn't *mean* anything about the semantic interpretation of the item
at that position. It's for this reason that a list is mutable: since
the index of an item has no semantic meaning, inserting new items or
removing them from anywhere in the sequence doesn't alter the meaning
of the structure.

James Tauber explains further:

    <URL:http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists>

-- 
 \     "You can be a victor without having victims."  -- Harriet Woods |
  `\                                                                   |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list