extend() method in Python

The extend() method in python adds more number of elements to the right side of an existing deque object from an iterable

Deque Updated 19 Mar 2017 1 min read

Method Name

extend

Method Signature

extend(iterable)

Method Overview

  • extend() add more number of elements to a deque object from an iterable object.

  • The newly added elements are added to the right side of the deque.

Example

python
import collections

oddNumbers  = (1, 3, 5, 7, 9)

oddDeque    = collections.deque(oddNumbers)

print("Odd numbers:")

print(oddDeque)

moreOddNumbers  = (11, 13, 15, 17, 19)

oddDeque.extend(moreOddNumbers)

print("More odd numbers:")

print(oddDeque)

Output

Output
Odd numbers:

deque([1, 3, 5, 7, 9])

More odd numbers:

deque([1, 3, 5, 7, 9, 11, 13, 15, 17, 19])