Python tuple count() method “returns the number of times the specified element appears in the tuple.”
Syntax
tuple.count(element)
Parameters
“element”: It is an element to count in the tuple.
Return Value
Python tuple count() method returns the number of occurrences of one element in the tuple.
Example 1: How to Use tuple.count() Method
# Declaring tuple
marks = (75, 64, 91, 67, 75, 64)
# Now we will print the tuple
print("Tuple values are: ", marks)
# We will count the occurence of 75 and 64
print("Frequency of 75 is :", marks.count(75))
print("Frequency of 64 is :", marks.count(64))
# Printing total marks
print("Total marks : ", sum(marks))
Output
Tuple values are: (75, 64, 91, 67, 75, 64)
Frequency of 75 is : 2
Frequency of 64 is : 2
Total marks : 436
Example 2: Count List and Tuple Elements Inside Tuple
# tuple containing list and tuples
random = ('b', ('b', 'c'), ('b', 'c'), [5, 6])
# count element ('b', 'c')
count = random.count(('b', 'c'))
print("The count of tuple ('b', 'c') is:", count)
# count element [5, 6]
count = random.count([5, 6])
print("The count of list [5, 6] is:", count)
Output
The count of tuple ('b', 'c') is: 2
The count of list [5, 6] is: 1
Example 3: Counting elements that do not exist in the Tuple
tup_1 = (10, 11, 21, 31, 21, 31, 19, 3, 2)
tup_2 = ('I', 'am', 'a',
'disco', 'dancer', 'you', 'know')
# count the appearance of 3
res = tup_1.count(21)
print('Count of 21 in tup_1 is:', res)
# count the appearance of python
res = tup_2.count('Kim')
print('Count of Kim in tup_2 is:', res)
Output
Count of 21 in tup_1 is: 2
Count of Kim in tup_2 is: 0
Example 4: Counting occurrences of Tuples in a Tuple
main_tup = ((1, 2), ('a', 'b'), (1, 2),
('c', 'd'), ('a', 'b'))
count_tup = main_tup.count((1, 2))
print(count_tup)
count_xy = main_tup.count(('x', 'y'))
print(count_xy)
Output
2
0
That’s it.