Tuples and lists are two very similar data structure types that are utilized to store the data. But the tuples are immutable and the lists are mutable. While working with these data structures we sometimes need to perform different operations such as counting elements, removing elements, or appending elements. To count the occurrences of the specified element value of the given tuple, the “tuple.count()” method is utilized in Python.
The following contents will be discussed in this write-up to provide you with an overview of the Python “tuple.count()” method:
What is the Python “tuple.count()” Method?
- Example 1: Retrieving the Count of Specific Tuple Value
- Example 2: Retrieving the Count of Tuple and List Inside the Tuple
- Example 3: Retrieving the Count of String of Tuple in Python
- Example 4: Retrieving the Count of Tuple Value That Does Not Exist
- Example 5: Retrieving the Count Value of Spaces and Symbols in a Tuple
- Example 6: Retrieving the Count of Multiple Items in a Tuple
What is the Python “tuple.count()” Method?
The “tuple.count()” method in Python retrieves the total occurrences of the particular element value present in the tuple.
Syntax
Parameters
In the above syntax, the “tuple.count()” method takes the “value” parameter. This parameter indicates the element/item value required to search for in the given tuple.
Return Value
The “tuple.count()” method retrieves the integer value specifying the number of times the particular value appears in the given tuple.
Example 1: Retrieving the Count of Specific Tuple Value
In the following example code, we first declare the tuple with multiple element values. After that, the “tuple.count()” method retrieves the total occurrences of the passed element value. To display the output the “print()” function is employed.
# Counting Element Value
res = tuple_1.count(15)
print(res)
The above code displays the following integer value:
Example 2: Retrieving the Count of Tuple and List Inside the Tuple
In the following code, the “tuple.count()” method takes the tuple or list value as an argument and retrieves the count that specifies the number of occurrences found in the tuple.
print('Counting Nested Tuple Element:', tuple_1.count(('x', 'y')))
print('\nCounting List Element in Tuple:', tuple_1.count([6, 9]))
The above code shows the below output:
Example 3: Retrieving the Count of String of Tuple in Python
Here, in this example, we initially declared the tuple value containing the string. Next, the “tuple.count()” method takes the string value “Python” and counts the total number of occurrences found in the specified tuple.
print('Tuple Value:')
print(tuple_1)
# Counting String Element Value
output = tuple_1.count('Python')
print('\nCount of String in Tuple:', output)
The above code displays the below snippet:
Example 4: Retrieving the Count of Tuple Value That Does Not Exist
Sometimes, we find the count of the particular value of the tuple that does not exist. For example, here we passed the “25” integer value to the “tuple.count()” method. Because this value is not found in the given tuple the “tuple.count()” method retrieves the “0” value to the console.
output = tuple_1.count(25)
print('Count of 25 in Given Tuple:', output)
The below output shows that the particular value does not present:
Example 5: Retrieving the Count Value of Spaces and Symbols in a Tuple
There are cases when we use empty spaces or any symbols in a tuple value. For example, in this code, we first declared the tuple with a number, empty spaces, and symbols. After that, we first passed an empty space to the “tuple.count()” method to retrieve the count occurrence of empty spaces in the tuple. After that, we passed the “@” symbol to the “tuple.count()” method to retrieve the occurrence of the specified symbol in the given tuple.
print(tuple_1)
print("\nSpaces Count in Tuple Value:", tuple_1.count(''))
print("\nSymbol Count in Tuple Value:", tuple_1.count('@'))
The above code retrieves the following output:
Example 6: Retrieving the Count of Multiple Items in a Tuple
We can also count the multiple elements of the specified tuple. To do this, initially, we import the “Counter” constructor method from the collections module. Next, we created the tuple with multiple values. Lastly, the “Counter()” method is called on the tuple and retrieves the occurrences of each of the tuple elements.
tuple_1 = ('x', 'y', 'z', 'y', 'x', 'x', 'x')
print(tuple_1, '\n')
print(Counter(tuple_1))
The below snippet shows the count of each element:
Conclusion
In Python, the “tuple.count()” method retrieves the total number of occurrences of elements present or found in the specified tuple. We can pass the specified element value such as string, integer, or others to retrieve the total occurrences. The spaces or symbols count value can also be retrieved from the tuple using the “tuple.count()” method. This guide delivered a detailed tutorial on Python’s “tuple.count()” method utilizing numerous examples.