FAQ
Hi,
Is there a simpler way to yield all elements of a sequence than this?
for x in xs:
yield x

I tried googling but fond only the other direction (turning a generator
into a list with "list(my_generator())".

Sebastian

Search Discussions

  • Chris Rebert at Oct 23, 2010 at 10:22 pm

    On Wed, Oct 20, 2010 at 3:27 PM, Sebastian wrote:
    Hi,
    Is there a simpler way to yield all elements of a sequence than this?
    for x in xs:
    ? ?yield x
    Not presently. There's a related PEP under discussion though:
    PEP 380: Syntax for Delegating to a Subgenerator
    http://www.python.org/dev/peps/pep-0380/

    It would let you write:
    yield from xs

    Cheers,
    Chris
  • Stefan Schwarzer at Oct 24, 2010 at 6:58 pm
    Hi Sebastian,
    On 2010-10-21 00:27, Sebastian wrote:
    Is there a simpler way to yield all elements of a sequence than this?
    for x in xs:
    yield x
    Can you give an example where you would need this? Can't
    you just iterate over the sequence? If you really need an
    iterator, you can use `iter(sequence)`:
    my_list = [1, 2, 3]
    i = iter(my_list)
    i.next()
    1
    i.next()
    2
    i.next()
    3
    i.next()
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    StopIteration

    This works at least with Python's built-in sequences (and
    dictionaries and sets, but note that these don't have an
    obvious order).

    Stefan
  • Cameron Simpson at Oct 24, 2010 at 11:08 pm

    On 24Oct2010 20:58, Stefan Schwarzer wrote: | On 2010-10-21 00:27, Sebastian wrote:
    Is there a simpler way to yield all elements of a sequence than this?
    for x in xs:
    yield x
    Can you give an example where you would need this? Can't
    you just iterate over the sequence?
    The usual example is when the sequence comes from inside.
    Example, closely resembling some code from on of my projects:

    def leaves(N):
    if N.isleaf:
    yield N
    for subN in N.subnodes:
    for leaf in leaves(subN):
    yield leaf

    which walks a tree structure returning leaf nodes.

    The point is that you're calling leaves() on the subnode and yiled them
    directly to the outside. The caller may not even know there are "subnodes".

    Cheers,
    --
    Cameron Simpson <cs at zip.com.au> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    The Borg assimilated my race and all I got was this lousy tagline.
    - Cath Lawrence <Cath_Lawrence at premium.com.au>
  • Stefan Schwarzer at Oct 24, 2010 at 11:37 pm
    Hi Cameron,
    On 2010-10-25 01:08, Cameron Simpson wrote:
    On 24Oct2010 20:58, Stefan Schwarzer wrote:
    On 2010-10-21 00:27, Sebastian wrote:
    Is there a simpler way to yield all elements of a sequence than this?
    for x in xs:
    yield x
    Can you give an example where you would need this? Can't
    you just iterate over the sequence?
    The usual example is when the sequence comes from inside.
    Example, closely resembling some code from on of my projects:

    def leaves(N):
    if N.isleaf:
    yield N
    for subN in N.subnodes:
    for leaf in leaves(subN):
    yield leaf

    which walks a tree structure returning leaf nodes.

    The point is that you're calling leaves() on the subnode and yiled them
    directly to the outside. The caller may not even know there are "subnodes".
    From the question and the code snippet the OP gave I assumed
    he meant that there already was a sequence (i. e. linear
    structure) to begin with.

    By the way, I think a well-known example of what you
    describe is `os.walk`.

    Stefan
  • Cameron Simpson at Oct 25, 2010 at 12:13 am

    On 25Oct2010 01:37, Stefan Schwarzer wrote:
    From the question and the code snippet the OP gave I assumed
    he meant that there already was a sequence (i. e. linear
    structure) to begin with.
    I suspected that was your interpretation.
    By the way, I think a well-known example of what you
    describe is `os.walk`.
    Indeed. I'm surprised it didn't occur to me.

    Cheers,
    --
    Cameron Simpson <cs at zip.com.au> DoD#743
    http://www.cskk.ezoshosting.com/cs/

    The only thing necessary for the triumph of evil is for good men to do nothing.
    - Edmund Burke (1729-1797)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedOct 20, '10 at 10:27p
activeOct 25, '10 at 12:13a
posts6
users4
websitepython.org

People

Translate

site design / logo © 2023 Grokbase