w3resource

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 presents in a deque object.

Sample Solution:

Python Code:

import collections
nums = (2,9,0,8,2,4,0,9,2,4,8,2,0,4,2,3,4,0)
nums_dq = collections.deque(nums)
print("Number of 2 in the sequence")
print(nums_dq.count(2))
print("Number of 4 in the sequence")
print(nums_dq.count(4))

Sample Output:

Number of 2 in the sequence
5
Number of 4 in the sequence
4

Flowchart:

Python Collections: Count the number of times a specific element presents in a deque object.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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 Python skills with w3resource's quiz


Python: Tips of the Day

Splits a multiline string into a list of lines

Example:

def tips_splitlines(s):
  return s.split('\n')

print(tips_splitlines('Pack\nmy box\nwith\nfive\ndozen liquor jugs.\n'))

Output:

['Pack', 'my box', 'with', 'five', 'dozen liquor jugs.', '']