How to Use Tuple count() Method in PythonPython Basics


The tuple.count() method returns the number of occurrences of an element in a tuple.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

temp_tuple = ('a', 'a', 'a', '1', '1', '2', '2', '2', '4','4','5','5')

Count = temp_tuple.count('a')
print('The number of a is: ', Count)
Count = Temp_tuple.count('1')
print('The number of 1 is: ', Count)
Count = Temp_tuple.count('4')
print('The number of 4 is: ', Count)

Output:

The number of a is:  3
The number of 1 is:  2
The number of 4 is:  2

Syntax

tuple.count(x)

Parameters

Name Description
x The element whose count is to be found

Return Value

It returns the number of occurrences of a given element in the tuple.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us