"Tuples are for heterogeneous data, lists are for homogeneous data."

Donn Cave donn at u.washington.edu
Wed Mar 12 12:28:39 EST 2003


Quoth Lulu of the Lotus-Eaters <mertz at gnosis.cx>:
| Arthur <ajsiegel at optonline.net> wrote previously:
|> The subject line is a Guido quote from today's dev list.  Is the point
|> Guido making performance related, or is it something else?  I have used
|> lists extensively for heterogenous data, and wonder what it is I am
|> losing by so doing.
|
| I think perhaps you are thinking of "homogeneous" in too narrow a sense.
|
| Data that is homogeneous in a Pythonic way isn't necessarily all
| integers, or all strings.  Rather, a list is a bunch of things that you
| might loop over, treating each element in the "same way."  Now this same
| way might not be entirely the *same*:  for example, if you call a method
| on each list element, the method might wind up dispatching to different
| code depending on the type of the element.  Or you might even have some
| branches within the loop body (if's, try's, etc)... but even then,
| there's something "the same" involved in that every element is a
| candidate for the same branch conditions, albeit different ones choose
| different paths.
|
| Tuples, in contrast, are more often used as *records*, i.e. related
| information about a common thing.  The fields of a record need not be
| different data *types*, they just represent different information.  For
| example, age, weight, and SSN might all be stored as integers, but the
| -meaning- of those three positions in a tuple is different.

I wonder if it would be useful to think about this in terms of more
specific types implemented as sequences.  The 9-tuple result of
time.localtime(t), list of strings in sys.argv, etc. are each in
a practical sense distinct data structures, and though not distinct
types at the Python implementation level they still have their own
rules and applications.

If you look at it that way, you can observe that sys.argv[1:] is
of the same type as sys.argv[:], but tm[1:] is not of the same
type as tm[:].  That is, the result of time.localtime() is not
interchangeable in any sense with a sub-slice of itself, and this
is true in general of tuples used as records.  But lists used as
sequential storage structures are homogeneous in the sense that
they can be sliced and spliced into the same kinds of lists.

	Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list