iterating over a deque in two directions efficiently in Python -
i have deque, let's call deq
. need iterate on both ends, , not modifying @ during these iterations.
naturally, don't want create another deque. i've considered reversed
, don't know if creates copies. if, example, write:
reversed_deq = reversed(deq)
will reference exact same memory locations, iterate on in reverse, without using more memory/time?
that seems logical way go double-ended queue, want make sure i'm not missing anything.
i can't find code deque (usually have "python equivalent" of these things, couldn't find it), , reason - no matter run - timeit
gives me between 15 , 16 ns (for try time, not this)
from c source reversed([deque]) returns reverse iterator, no copies or memory allocation. [deque].reverse() reverse in place.
Comments
Post a Comment