tuples vs lists

Donn Cave donn at u.washington.edu
Wed Sep 17 12:19:03 EDT 2003


In article <3F68625F.984871CE at engcorp.com>,
 Peter Hansen <peter at engcorp.com> wrote:

> Ivan Voras wrote:
> > 
> > Peter Hansen wrote:
> > > Ivan Voras wrote:
> > 
> > >> I know that. I was just wandering if they also behave faster. Lists
> > >> are only interesting to me in case I need mutability.
> > >
> > > I'd suggest you have that backwards.  Lists should _always_ be
> > > interesting
> > > to you.  Tuples should be interesting only in the case where you need
> > > *immutability*.
> > 
> > I disagree. :) I would always use tuples except when I explicitely need
> > mutability. That doesn't meen that I would intentionaly (badly) restructure
> > code just to use tuples, only that in many cases my structures don't need 
> > to
> > be modified.
> 
> That's your personal choice, I suppose, and I won't try any more to dissuade
> you any more except to point out that most Python programmers, I believe, do 
> not see tuples as their first choice, but use them only in certain special 
> cases.

These two positions may be more similar than different.  The
notion of a sequence that never needs to be modified is certainly
a special case, and if we were looking at the actual applications
for these sequences we might all agree that they're more or less
appropriately cast as tuples.  One argues about principles like
this at some risk of silliness.

The thing that eventually pushed me towards lists and away from
tuples, for sequences of like items, is the notation.  It may
seem like a awfully prosaic consideration, but lists are usually
easier to mentally parse out of an expression, because the tuple's
parentheses are shared by other syntactical groupings.  And then
there's the awkward notation for a single-element tuple.  Lists
are easier to write and easier to read.

   Donn Cave, donn at u.washington.edu




More information about the Python-list mailing list