In this tutorial, we will look at how to count the frequency of a value in a deque in Python with the help of some examples.
deque in Python
collections module in Python comes with a
deque class that you can use to implement a stack or queue data structure in Python. Deques support thread-safe, memory efficient appends and pops from either side of the deque with approximately the same O(1) performance in either direction.
In simple terms, you can think of a
deque in Python as a list-like data structure but with efficient insertion and removal of items from either end (left and right). This makes them very useful when implementing a stack or queue functionality in Python.
How to get element frequency in a deque in Python?
deque class comes with a number of built-in functions to help perform common operations (like adding and removing elements).
To count the occurrences of a value in a deque in Python, use the deque
count() function. Pass the value as an argument.
The following is the syntax –
# count occurrences of "x" in deque "queue" queue.count(x)
It returns the number of elements in the deque that are equal to
Let’s now look at some examples of using the above syntax –
Example 1 – Count occurrences of a value deque
First, let’s create a deque with some elements.
from collections import deque # create a deque queue = deque([1, 2, 3, 4, 3, 5, 7]) # print the deque print(queue)
deque([1, 2, 3, 4, 3, 5, 7])
Here, we imported the
deque class from the
collections module and created a deque object,
queue with some numbers. You can see that the number
3 occurs two times in the above deque.
Now, let’s now count the occurrences of the value
3 in the above deque using code.
# count occurrences of 3 print(queue.count(3))
Here, we used the deque
count() function to get the frequency of the value
3 in the above deque. It returns
2 which is the number of times
3 occurs in the deque,
Example 2 – Count occurrences of a value not present in a deque
Let’s now try to find the frequency of a value not present in the deque.
We’ll use the same deque from above and pass
6 as the argument (the value
6 is not present in the deque).
# count occurrences of 6 print(queue.count(6))
We get 0 as the output because there are no values equal to
6 in the given deque object.
You might also be interested in –
- Python – Convert a deque of strings to a single string
- Python – Convert a deque to a list
- Python – Get the Index of the Maximum Value in Deque
- Python – Get the Index of the Minimum Value in Deque
- Python – Get the Maximum and the Minimum Value in a Deque
- Python – Check If a Deque is Empty
- Python – Get Value by Index in Deque
- Python – Create a deque copy
- Python – Rotate a Deque to the Right and Left
- Python – Extend deque to the left
- Python – Extend deque to the right
- Python – Get the Max Size of a Deque
- Python – Reverse a deque
- Python – Get Index of a Value in Deque
- Python – Remove Element by Value in Deque
- Python – Remove all elements from a deque (clear deque)
- Python – Append Element to a Deque
- Python – Append Element to the Left in Deque
- Python – Pop Element From Left in Deque
- Python – Pop Element From the End in Deque
- Python – Insert Element at a given Index in Deque
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.