[Tutor] Distinction between tuples and lists

Andre Engels andreengels at gmail.com
Thu Jan 1 18:33:23 CET 2009


On Thu, Jan 1, 2009 at 3:07 PM, Christopher Mutel <cmutel at gmail.com> wrote:
> Hello all-
>
> I stumbled across some discussion of why the fundamental difference
> between lists and tuples is not mutability, but hetero- versus
> homogeneous data, e.g.
>
> 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
> than heterogeneous sequences (or vice-versa for tuples)? In the end,
> from what I understand, the idea of homo/heterogeneity seems
> orthogonal to mutability, which is the main distinction emphasized by
> e.g. Learning Python.
>
> I would greatly appreciate any help provided,

Lists do have all kinds of operations that tuples do not have. Many of
those will change the order of the elements in the list, for example
by adding or removing an element, or exchanging some elements. Because
of this, many of those operations are only useful on homogenous
sequences - which means that the second element could also be the
first or the fourth, and would then still mean the same thing, though
in another place.

On the other hand, if I have a heterogenous sequence, that is, if what
is in the second position could either not appear on the fourth
position (because the second is an integer and the fourth a string,
for example), or would mean something completely different there (for
example if the second and fourth were both floats, but the second was
a price in dollars and the fourth a mass in kilograms), then many of
those operations make no sense (in fact, the only one I think _would_
make sense for heterogenous sequences is changing the value of the
element at a specified position). Thus, if you have a heterogenous
sequence, all those nifty operations on lists have no use, and
dropping them for getting the niceties of immutability (like usage as
the key in a dictionary) is getting something for almost nothing, thus
you might as well use a tuple.

--
André Engels, andreengels at gmail.com


More information about the Tutor mailing list