Home

appendleft

Appendleft is a method used by double-ended queue (deque) data structures to insert a new element at the left end of the sequence. A deque supports insertion and removal at both ends, allowing efficient addition at either the front or the back.

In Python's standard library, the collections.deque type provides appendleft, which inserts the element on the left

Common use cases include maintaining a dynamic front-loaded sequence, implementing a queue where elements may need

Other libraries and languages implement an equivalent operation with different names. In many languages that provide

side,
i.e.,
the
front.
The
counterpart
append
adds
to
the
right
side.
The
operation
runs
in
O(1)
time
on
average.
The
method
mutates
the
deque
in
place
and
returns
None.
If
the
deque
has
a
maximum
length
(maxlen)
and
is
full,
appending
to
either
end
will
discard
an
element
from
the
opposite
end
to
make
room.
to
be
prepended,
or
supporting
algorithms
that
require
inserting
at
the
front
while
maintaining
fast
access
to
both
ends.
It
can
be
convenient
for
building
a
sequence
in
reverse
order
without
reversing
at
the
end,
or
for
certain
cache
or
streaming
scenarios
where
new
items
arrive
at
the
front.
a
generic
deque,
the
method
is
called
push_front
or
push_left;
appendleft
is
a
naming
convention
most
closely
associated
with
Python's
collections.deque.