iterators and continuing after a StopIteration

R

Roald de Vries

Hi all,


I have a list that I'm iterating over, and during the iteration items
are appended. Moreover, it is iterated over in two nested loops. If
the inner loop comes to the end, I want the outer loop to append an
item. Is there a way to do this? Because once an iterator has raised a
StopIteration, it can not go to a next item anymore.

Aside question: is there a rationale for the current behavior? To me
it seems more natural to continue iteration after appending new items.


I want to use it for a graph walk.

nodes is a list of all nodes,
edges is an on the fly constructed list of edges in the order of
visiting,
initial_nodes is a list of (root) nodes

edges = []
edge_it = iter(edges)
for node in initial_nodes:
edges += node.leaving_edges
try:
while True:
edge = edge_it.next()
edges += edge.head.leaving_edges
except StopIteration:
pass


Thanks in advance, cheers, Roald
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top