Extend() Method In Python

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:

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:        

Odd numbers:

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

More odd numbers:

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

 

 


Copyright 2023 © pythontic.com