Python extend()

Anonymous contributor's avatar
Anonymous contributor
Published Oct 29, 2025
Contribute to Docs

The extend() method adds multiple elements to the right end of a deque from any iterable (like a list, tuple, or another deque). It modifies the deque in place and returns None.

  • 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.extend(iterable)

Parameters:

  • iterable: A sequence or iterable whose elements will be added to the right end of the deque.

Return value:

This method does not return any value. It modifies the deque in-place.

Example

In this example, extend() adds elements from a list and another deque to the right end of a deque:

from collections import deque
# Create a deque
dq = deque([1, 2, 3])
# Extend it
dq.extend([4, 5, 6])
print("Extending with list: ", dq)
#Extend it using another deque
dq.extend(deque([7, 8, 9]))
print("Extending with another deque: ", dq)

This example results in the following output:

Extending with list: deque([1, 2, 3, 4, 5, 6])
Extending with another deque: deque([1, 2, 3, 4, 5, 6, 7, 8, 9])

Codebyte Example: Extending a deque with a list and a tuple

In this example, extend() adds elements from a list and a tuple, demonstrating its ability to handle different iterables:

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