[Python-Dev] Bug or not? Different behaviour iterating list and collections.deque

Josiah Carlson jcarlson at uci.edu
Sun Jan 7 18:58:17 CET 2007


"Christos Georgiou" <tzot at mediconsa.com> wrote:
> 
> Hello, people. I am not sure whether this is a bug or intentional, so I 
> thought checking it with you before opening a bug. I will explain this 
> issue, but please understand this is not a question for help to change the 
> algorithm (this has been done already), so it's not a question of c.l.py. 
> It's a matter of discrepancy.
[snip]
> Forgive my piggy backing, but I forgot to include the only related post I 
> found, which did not clear things up for me:
> 
> http://groups.google.com/group/comp.lang.python/msg/e2dcb2362649a601


Lists are not deques, deques are not lists.  As stated in the post you
link to states, the behavior of lists and deques is different in the
same situation, so while one may "work" for your task, the other may not.

I would, generally speaking, concur with Rayomond's recommendation in
the post and say that mutation of the item you are iterating over is
confusing, and it certainly is likely far more tricky than you should be
when writing software so that people can use it.

In this case, you are making it even worse by attempting to merge a list
and deque, under the presumption that they should do the same thing. 
They don't.  They are documented not to do the same thing.  And you read
a post explaining that they don't do the same thing and cited it.


My suggestion: don't do what you are doing.  Input list, output list. 
Make your life simple.  Appending to a list is fast.


If you *need* to merge a list and deque, do to some insane requirements
by an organization, write your own wrapper that does what you think is
the right thing to do in this case.

 - Josiah



More information about the Python-Dev mailing list