Do this as a list comprehension?

Mensanator mensanator at aol.com
Sun Jun 8 03:03:09 CEST 2008


On Jun 7, 6:43�pm, "Terry Reedy" <tjre... at udel.edu> wrote:
> "John Salerno" <johnj... at gmailNOSPAM.com> wrote in message
>
> news:484ad07b$0$25721$607ed4bc at cv.net...| Mensanator wrote:
>
> |
> | > Surely enumerate() wasn't added to Python with no intention of
> | > ever being used.
> |
> | I see your reasons for preferring enumerate over zip, but I'm wondering
> | if using enumerate this way isn't a little hackish or artificial.
>
> It seems to be a difference of personal preference. �I see no reason to
> write a for loop (statement or expression) when a simple usage of basic
> builtins does the same. �Mensanator apparently does. �

*sigh* I never said a for..loop was preferable.
What I said was the answer to "Can I do this with
a list comprehension?"

I never said you shouldn't use the builtins.

What I DID say was that how the builtins actually
work should be understood and it APPEARED that the
OP didn't understand that. Maybe he understood that
all along but his example betrayed no evidence of
that understanding.

> So it goes.

So I was trying to help the guy out. So sue me.

>
> Because zip stops when the first iterator is exhausted, the original zip
> with range can be pretty well future proofed with a high stop value.
>
> zip(range(9,2000000000), iterable)

Oh, dear. You didn't actually try this, did you?

>
> Of course, a non-1 step can be added to the range.

I would hope so, otherwise you're going to have
a problem creating a list with two billion integers.
You might want to try xrange() in your production code.

>
> tjr




More information about the Python-list mailing list