Python: Count the number of times a specific element presents in a deque object
Python Collections: Exercise-12 with Solution
Write a Python program to count the number of times a specific element appears in a deque object.
Sample Solution:
Python Code:
# Import the collections module to use the deque data structure
import collections
# Create a tuple 'nums' containing a sequence of numbers
nums = (2, 9, 0, 8, 2, 4, 0, 9, 2, 4, 8, 2, 0, 4, 2, 3, 4, 0)
# Create a deque 'nums_dq' from the 'nums' tuple
nums_dq = collections.deque(nums)
# Print a message to indicate the display of the count of number 2 in the sequence
print("Number of 2 in the sequence")
# Count and print the number of occurrences of 2 in 'nums_dq'
print(nums_dq.count(2))
# Print a message to indicate the display of the count of number 4 in the sequence
print("Number of 4 in the sequence")
# Count and print the number of occurrences of 4 in 'nums_dq'
print(nums_dq.count(4))
Sample Output:
Number of 2 in the sequence 5 Number of 4 in the sequence 4
Flowchart:
Python Code Editor:
Previous: Write a Python program to copy of a deque object and verify the shallow copying process.
Next: Write a Python program to rotate a Deque Object specified number (positive) of times.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-12.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics