List as FIFO in for loop
duncan smith
buzzard at urubu.freeserve.co.uk
Sat Mar 8 10:52:11 EST 2008
malkarouri wrote:
> Hi everyone,
>
> I have an algorithm in which I need to use a loop over a queue on
> which I push values within the loop, sort of:
>
> while not(q.empty()):
> x = q.get()
> #process x to get zero or more y's
> #for each y:
> q.put(y)
>
> The easiest thing I can do is use a list as a queue and a normal for
> loop:
>
> q = [a, b, c]
>
> for x in q:
> #process x to get zero or more y's
> q.append(y)
>
> It makes me feel kind of uncomfortable, though it seems to work. The
> question is: is it guaranteed to work, or does Python expect that you
> wouldn't change the list in the loop?
>
I have used exactly the same approach. I think it's a clean (even
elegant) solution. I'd be surprised if it ceased to work in some future
implementation of Python, but I don't know if that's absolutely guaranteed.
Duncan
More information about the Python-list
mailing list