Support comparison to lists in `collections.deque`

Deques should be comparable to lists.

Otherwise one would end up doing…

list(mydeque) == mylist

…which is very inefficient if the deque is big, or…

all(x == y for x, y in zip(mydeque, mylist))

…which is very cumbersome.

Why not having a proper __eq__ in the deque structure to allow mydeque == mylist?

What do you think? Thanks!

One less cumbersome version of the all solution, at least, is

all(map(operator.eq, mydeque, mylist))

Given the different use-cases for lists and deques, it’s not immediately obvious why you would need to make such comparisons often enough to require direct comparison. (Both types are iterable, but iterables are in general not comparable to each other.)

1 Like

[1,2,3], (1,2,3), and range(1,4) are unequal to each other because while the contents are equal, and in the same order, one mostly cannot do the same things with each of them. One cannot even add them. The same is largely true of add 3 and deques. One would need a strong reason to special-case this one pair of different sequence objects.

1 Like

Just a quick note: both all(x == y for x, y in zip(mydeque, mylist)) and all(map(operator.eq, mydeque, mylist)) are incorrect.

You need zip(..., strict=True).


I think this may not be necessary, after all, the basic type has not tried to do this:

assert not (1,2,3) == [1,2,3]