Tuple comprehension

brueckd at tbye.com brueckd at tbye.com
Fri Apr 12 11:20:36 EDT 2002


On Thu, 11 Apr 2002, David Eppstein wrote:

> In article <slrnabchgs.c8l.mlh at vier.idi.ntnu.no>,
>  mlh at vier.idi.ntnu.no (Magnus Lie Hetland) wrote:
>
> > I think the only _real_ reason to have tuples at all is that they are
> > immutable (and therefore usable as dictionary keys). And a slight
> > efficiency advantage over lists, maybe...
>
> I'm guessing (without looking at the source) that tuples use less memory
> because they don't need the extra room for growth that lists do.
> But my mental model of them is just "immutable list".

Yes, this is where the difference in opinion lies: as immutable lists,
tuple comprehensions can make sense. I like the mental model of tuples
put forth by the Python tutorial:

[5.3] "Tuples have many uses, e.g. (x, y) coordinate pairs, employee
records from a database, etc."

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.

In this sense tuple comprehensions are less valuable because they'd really
only be useful in cases where all the members are homogeneous, where it'd
make sense to apply the same sort of operation to each item. There's a few
obvious cases where this happens (coordinates, open/high/low/close prices
for a stock), but they don't seem all that common.

-Dave






More information about the Python-list mailing list