How to iterate a sequence, with skipping the first item?

Discussion in 'Python' started by ray, Aug 12, 2008.

  1. ray

    ray Guest

    A container object provides a method that returns an iterator object.
    I need to iterate the sequence with that iterator, but need to skip
    the first item. I can only iterate the whole sequence with:
    for x in container.iterChildren():
    How to skip the first item? It seems that it's a simple question.
    Could somebody help me? Thanks.
     
    ray, Aug 12, 2008
    #1
    1. Advertising

  2. ray

    alex23 Guest

    On Aug 12, 4:51 pm, ray <> wrote:
    > A container object provides a method that returns an iterator object.
    > I need to iterate the sequence with that iterator, but need to skip
    > the first item. I can only iterate the whole sequence with:
    > for x in container.iterChildren():
    > How to skip the first item? It seems that it's a simple question.
    > Could somebody help me? Thanks.


    Does the for loop -have- to contain the reference to iterChildren? If
    you can modify that, you can always manually step past the first item:

    children = container.iterChildren()
    children.next()
    for x in children: ...
     
    alex23, Aug 12, 2008
    #2
    1. Advertising

  3. i = iter(container.iterChildren())
    i.next()
    for x in i:
    ...

    On Tue, Aug 12, 2008 at 2:51 AM, ray <> wrote:
    > A container object provides a method that returns an iterator object.
    > I need to iterate the sequence with that iterator, but need to skip
    > the first item. I can only iterate the whole sequence with:
    > for x in container.iterChildren():
    > How to skip the first item? It seems that it's a simple question.
    > Could somebody help me? Thanks.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Read my blog! I depend on your acceptance of my opinion! I am interesting!
    http://ironfroggy-code.blogspot.com/
     
    Calvin Spealman, Aug 12, 2008
    #3
  4. ray

    Paul Rubin Guest

    ray <> writes:
    > for x in container.iterChildren():
    > How to skip the first item? It seems that it's a simple question.
    > Could somebody help me? Thanks.


    First solution:

    c = container.iterChildren()
    c.next() # skip first item
    for x in c: ...

    Second solution:

    from itertools import islice
    for x in islice(container.iterChildren(), 1, None): ...

    I like the second solution better but it's a matter of preference.
     
    Paul Rubin, Aug 12, 2008
    #4
  5. En Tue, 12 Aug 2008 03:51:17 -0300, ray <> escribi�:

    > A container object provides a method that returns an iterator object.
    > I need to iterate the sequence with that iterator, but need to skip
    > the first item. I can only iterate the whole sequence with:
    > for x in container.iterChildren():
    > How to skip the first item? It seems that it's a simple question.
    > Could somebody help me? Thanks.


    it = container.iterChildren()
    it.next() # consume first item
    for x in it:
    # process remaining items

    --
    Gabriel Genellina
     
    Gabriel Genellina, Aug 12, 2008
    #5
  6. ray

    ray Guest

    Thanks you guys. Now I understand the -for- loop mechanism.
     
    ray, Aug 12, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,106
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,462
    runescience
    Feb 9, 2006
  3. Johannes Zellner
    Replies:
    1
    Views:
    503
    Alex Martelli
    Jan 17, 2006
  4. stef mientki
    Replies:
    13
    Views:
    646
    stef mientki
    Oct 20, 2007
  5. John
    Replies:
    4
    Views:
    922
    RedGrittyBrick
    Apr 1, 2008
Loading...

Share This Page