Tuple comprehension

brueckd at tbye.com brueckd at tbye.com
Fri Apr 12 12:57:21 EDT 2002


On Fri, 12 Apr 2002, David Eppstein wrote:

> On 4/12/02 8:20 AM -0700, brueckd at tbye.com wrote:
> > The implication is that tuples group bits of often dissimilar but related
> > information that, grouped together, represent a unique entity - sort of a
> > poor man's object instance.
>
> But there's nothing preventing a list from holding inhomogeneous items,
> either.

Yes (and, once again, in that case the usefulness of comprehensions break
down), but in the general sense they are most powerful when there *is*
some common feature to the items in the list. Obviously the common ground
can range from quite specific (a list of integers) to quite broad (a list
of whatevers that this function is capable of processing). If the items
don't have this sort of relation, why do you have them in a list anyway?

>  So I don't see how this is a difference between tuples and lists,
> unless it's just one of standardized style.

The essence of this discussion is that it *is* more than a difference of
just standardized style - tuples were built for one thing and lists
another, albeit with a certain amount of overlap. Tuples work well in the
record-like or dictionary key scenario, while lists are more general
purpose, for use when you need to apply similar processing to a lot of
"stuff".

If tuples don't seem to do all you want it *might* indicate that an
enhancement to the language is in order, but maybe it means that a
different data type would be more appropriate.

Spoons and forks solve similar problems, and in many cases it doesn't
matter which you choose to use even though they were designed to solve
slightly different problems. If your ice cream refuses to cooperate with
your fork it *might* mean you need a better fork, but maybe it means you
should use a spoon (or a spork[1]).

-Dave

[1] http://www.sonic.net/~ian/Spork/spork.faq.html






More information about the Python-list mailing list