Python appendleft()

msafarookhi's avatar
Published Oct 22, 2025
Contribute to Docs

The appendleft() method adds an element to the left end (front) of a collections.deque object. This is useful for managing elements in a queue-like structure, where items are added and removed from opposite ends.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

deque.appendleft(element)

Parameters:

  • element: The item to add to the left end (front) of the deque.

Return value:

  • None. The deque is modified in place.

Example: Adding elements to the left side of a deque using appendleft()

In this example, appendleft() adds elements to the left end of a deque, demonstrating how items are prepended to the front:

from collections import deque
# Create a deque
my_deque = deque([1, 2, 3])
print(f"Initial deque: {my_deque}")
# Append an element to the left
my_deque.appendleft(0)
print(f"Deque after appendleft(0): {my_deque}")
# Append another element
my_deque.appendleft(-1)
print(f"Deque after appendleft(-1): {my_deque}")

The output of this code is:

Initial deque: deque([1, 2, 3])
Deque after appendleft(0): deque([0, 1, 2, 3])
Deque after appendleft(-1): deque([-1, 0, 1, 2, 3])

Codebyte Example: Using appendleft() for Dynamic Queue Operations

In this example, appendleft() is used to add elements to the front of a deque, simulating dynamic queue operations where new items arrive at the front:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours