Re: Lists and Tuples

From: Douglas Alan (nessus_at_mit.edu)
Date: 12/05/03


Date: Fri, 05 Dec 2003 02:37:22 -0500

David Eppstein <eppstein@ics.uci.edu> writes:

> That's true, but another answer is: you should use tuples for short
> sequences of diverse items (like the arguments to a function). You
> should use lists for longer sequences of similar items.

I disagree. You should use a tuple when you wish to not change the
contents once you have constructed the sequence, and otherwise you
should use a list. Using a tuple can make your code clearer by
letting the reader know from the beginning that the contents won't be
changing.

Fredrik Lundh actually called me names a couple years back for
asserting this, but Python luminary (and rude fellow) or not, he is
dead wrong.

You don't have to take my word for it, though, since Python itself
uses tuples in this manner, in the form of the container used for
excess arguments (excess arguments certainly don't have to be short,
and they are generally homogeneous, not heterogeneous), and Python
Mega Widgets (for example) is littered with code that looks like:

   optiondefs = (
       ('initwait', 500, None), # milliseconds
       ('label_background', 'lightyellow', None),
       ('label_foreground', 'black', None),
       ('label_justify', 'left', None),
       ('master', 'parent', None),
       ('relmouse', 'none', self._relmouse),
       ('state', 'both', self._state),
       ('statuscommand', None, None),
       ('xoffset', 20, None), # pixels
       ('yoffset', 1, None), # pixels
       ('hull_highlightthickness', 1, None),
       ('hull_highlightbackground', 'black', None),
   )

In the above case we see tuples being used both as records *and* as an
arbitrary-length sequence of homogenious elements. Why is a tuple
being used in the latter case, rather than a list? Because the
sequence isn't going to be modified.

|>oug



Relevant Pages

  • Re: Interface of the set classes
    ... >>It's about the interface of the set classes as defined in the PEP 218. ... the sets implements the sequence concept but without the impossible ... I don't know why this is different in Python! ... this interface incompatible with lists. ...
    (comp.lang.python)
  • Re: 2.2.2 Annoyance
    ... At the C implementation level, Python distinguishes sequences (lists, ... If you're implementing a sequence in C, ... handles a slice object! ...
    (comp.lang.python)
  • Classical FP problem in python : Hamming problem
    ... I couldn't resist to test the new Python features about ... laziness on a classical FP problem, i.e. the "Hamming" problem. ... The name of the game is to produce the sequence of integers satisfying the ... -- Merges two infinite lists ...
    (comp.lang.python)
  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • Re: Subclassing list the right way?
    ... about how overriding methods on them will affect other method calls. ... special interpretation of negative indexes (if the class wishes to ... outside the set of indexes for the sequence (after any special ... either write a lot of code or restrict the way you use lists to a ...
    (comp.lang.python)