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

Paul Rubin http
Tue Aug 12 03:02:06 EDT 2008


ray <hanlray at gmail.com> 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.



More information about the Python-list mailing list