Python: Add more number of elements to a deque object from an iterable object
9. Add More Elements to a Deque from an Iterable Object
Write a Python program to add more elements to a deque object from an iterable object.
Sample Solution:
Python Code:
# Import the collections module to use the deque data structure
import collections
# Create a tuple 'even_nums' containing even numbers 2, 4, 6, 8, and 10
even_nums = (2, 4, 6, 8, 10)
# Create a deque 'even_deque' from the 'even_nums' tuple
even_deque = collections.deque(even_nums)
# Print a message to indicate that even numbers are being displayed
print("Even numbers:")
# Print the 'even_deque' containing even numbers
print(even_deque)
# Create another tuple 'more_even_nums' containing additional even numbers
more_even_nums = (12, 14, 16, 18, 20)
# Extend the 'even_deque' with the 'more_even_nums' tuple
even_deque.extend(more_even_nums)
# Print a message to indicate that more even numbers are being displayed
print("More even numbers:")
# Print the updated 'even_deque' containing both sets of even numbers
print(even_deque)
Sample Output:
Even numbers: deque([2, 4, 6, 8, 10]) More even numbers: deque([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to extend a deque with elements from a list using the extend() method.
- Write a Python program to merge two deques by adding elements of one into another using extend().
- Write a Python program to add even numbers from a range to a deque and then append additional even numbers from another iterable.
- Write a Python program to use extendleft() to add elements from an iterable in reverse order into a deque.
Go to:
Previous: Write a Python program to create a deque from an existing iterable object.
Next: Write a Python program to remove all the elements of a given deque object.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.