w3resource

Python: Add more number of elements to a deque object from an iterable object

Python Collections: Exercise-9 with Solution

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:

Flowchart - Python Collections: Add more number of elements to a deque object from an iterable object.

Python Code Editor:

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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/collections/python-collections-exercise-9.php