Tuple question

Alex Martelli aleaxit at yahoo.com
Sat Sep 4 11:00:30 CEST 2004

Peter Hansen <peter at engcorp.com> 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.)

tuple(somegenerator(blah)) will work excellently well.  In 2.4, you can
even often code that 'somegenerator' inline as a generator
comprehension.  So this 'having the info in a list' argument sounds just
totally bogus to me.

Say I want to work with some primes and I have a primes generator.
Primes aren't going to change, so a tuple is a natural.  I start with,

ps = tuple(itertools.islice(primes(), 999999))

...and then I'm stumped because I can't index into ps to find, say, the
progressive number of some given prime N by ps.index(N).  How silly,
having to keep ps a list, i.e. mutable (when it intrinsically isn't)
just to be able to index into it!  [I can usefully exploit ps's
sortedness via module bisect... ignoring the latter's specs and docs
that keep screamign LISTS, bisect.bisect DOES work on tuples... but I
wouldn't feel comfy about that surviving, given said docs and


More information about the Python-list mailing list