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

R

ray

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.
 
A

alex23

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: ...
 
P

Paul Rubin

ray said:
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.
 
G

Gabriel Genellina

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top