Account
Categories

Python Tuple count() Method


Definition:

If you need to count the number of times a value appears in a tuple, then you can use the count() method.

Now you will clearly understand the method through the flowchart.

Tuple →  t = (17, 20, 20, 19, 20)

Index :    0      1      2      3      4  
           ↓      ↓      ↓      ↓      ↓
Value :   17     20     20     19     20  

Method →  t.count(20)

Counting →   20   20   20
             ↑     ↑     ↑
            (1)   (2)   (3)

Result →  3

Syntax

tuple_name.count(value)

Example:

t = (17, 20, 20, 19, 20)
result = t.count(20)
print(result)

Output:

Exercise

Create a tuple t = (5, 10, 5, 15, 5) and count how many times 5 appears.