Python count()

MamtaWardhani's avatar
Published Jan 20, 2026
Contribute to Docs

The count() method of a collections.deque object counts how many times a given value appears in the deque. It provides a direct way to determine the frequency of a specific element within the deque container.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

deque_object.count(value)

Parameters:

  • value: The element whose occurrences are to be counted in the deque.

Return value:

Returns an integer representing the number of elements in the deque that are equal to value.

Example 1: Using count() to find repeated values

In this example, the count() method is used to find how many times the value 3 appears in a deque:

from collections import deque
dq = deque([1, 2, 3, 3, 4, 3, 5])
print(dq.count(3))

The output of this code is:

3

Example 2: Counting non-existent elements returns zero

In this example, the method counts an element not present in the deque, returning zero:

from collections import deque
dq = deque([10, 20, 30, 40])
print(dq.count(100))

The output of this code is:

0

Codebyte Example: Using count() with string elements

In this example, the count() method is used to compute the frequency of a specific item in a deque of strings representing colours:

Code
Output

Frequently Asked Questions

1. What is the time complexity of the count() method in Python deque?

The count() method has O(n) time complexity, where n is the number of elements in the deque. This is because the method must iterate through all elements in the deque to count occurrences of the specified value.

2. What does count() return if the element is not found in the deque?

The count() method returns 0 if the specified element is not present in the deque. It does not raise an exception or return None, it simply indicates that there are zero occurrences of that value.

3. How does count() determine equality when counting elements?

The count() method uses the equality operator (==) to compare elements. For custom objects, it will use the object’s __eq__() method if defined. Two objects are considered equal if obj1 == obj2 evaluates to True.

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours