Tuple question

Peter Hansen peter at engcorp.com
Mon Sep 6 15:11:25 EDT 2004


Bryan Olson wrote:
> Peter Hansen wrote:
>  > Consider, for example, that one actually has to build the
>  > tuple in the first place... how can you do that without
>  > having the info in a list to begin with?  (I'm sure there
>  > are ways if one is ingenious, but I think the answers
>  > would just go to prove the point I was making.)
> 
> x = (1, 2)
> y = (3, 4)
> print x + y

In answer to this and to Alex' response, I should say that
in the context of whether using a tuple for the final
storage saves memory or not, neither response means much.

I said "without having the info in a list" but what I
meant to say was "without having the information already
stored elsewhere before it is put in a tuple".  The
above just has two tuples which add up in memory usage
to the same amount as the tuple you end up with, meaning
that just prior to the deletion of the two temporary
tuples (if that even happens) you are using *twice*
the memory you need to use.  Clearly that doesn't
help you much if memory is scarce.

Alex shows use of a generator... fine, but how do you
build the tuple without storing up the results of the
generator first somewhere else?  You can't preallocate the
space for the tuple if you don't know how long it will
be, but you have to preallocate the space for a tuple
(I believe, in the interpreter anyway, if not at the
programmer level) so you must therefore be storing the
entire output of the generator somewhere just prior
to the tuple creation: same problem as above.

I know Alex knows all this (or has some additional
info that I don't have and which he'll shortly provide),
so I can only assume he was reacting only to my poor
choice of wording with 'list' and/or was ignoring the
context of the discussion (memory usage).

-Peter



More information about the Python-list mailing list