On Mon, Aug 7, 2017 at 4:14 PM, Chris Angelico <ros...@gmail.com> wrote:

> On Tue, Aug 8, 2017 at 5:30 AM, Soni L. <fakedme...@gmail.com> wrote:
> > The generator syntax, (x for x in i if c), currently always creates a new
> > generator.


that's what it's for -- I'm confused as to what the problem is.


> > {x for x in integers if 1000 <= x < 1000000} # never completes, because
> it's
> > trying to iterate over all integers
>

this is a set comprehension -- but what is "integers"? is it a generator?

in which case,  it should take an argument so it knows when to end. Or if
it's really that symple, that's what range() is for.


However, similarly, I find that sometimes I want to iterate over a slice of
a sequence, but do'nt want to actually make the slice first.

So there is itertools.islice()

If "integers" is a sequence:

{x for x in integers[1000:10000]}

makes an unneeded copy of that slice.

{x for x in itertools.islice(integers, 1000, 10000)}

will iterate on the fly, and not make any extra copies.

It would be nice to have an easier access to an "slice iterator" though --
one of these days I may write up a proposal for that.

-CHB

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

chris.bar...@noaa.gov
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to