Python Tuple count() Function
The Tuple count()
method returns he number of times a specified element occurs in a tuple.
Syntax
my_tuple.count(element)
count() Parameters
Python Tuple count()
method parameters:
Parameter | Condition | Description |
---|---|---|
element | Required | Any element (of type string, list, set, etc.) you want to search for |
count() Return Value
Python Tuple count()
function returns the number of times the specified element appears in the tuple.
Examples
Example 1: Counting occurrences of a Single Element in a Tuple with count()
The count()
method returns the number of times the specified element appears in the tuple.
my_tuple = (0, 1, 2, 3, 0, 1, 2, 3)
element_to_search = 3
result = my_tuple.count(element_to_search)
print(result) # Output: 2
output
2
If the element does not occur in the tuple, then zero is returned.
my_tuple = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
element_to_search = 10
result = my_tuple.count(element_to_search)
print(result) # Output: 0
output
0
Example 2: Counting Strings occurrences in a Tuple with count()
The count()
method can be used to count the number of occurrences of a strings in a tuple.
For example, let's count the number of occurrences of 'Anna'
my_tuple = ('David', 'Tom', 'Anna')
element_to_search = 'Anna'
result = my_tuple.count(element_to_search)
print(result) # Output: 1
output
1
Example 3: Counting Lists occurrences in a Tuple with count()
The count()
method can be used to count the number of occurrences of a list in a tuple.
In the following example, the list [3, 4]
appears twice in the tuple:
my_tuple = ('a', ('a', 'b'), [3, 4], ('a', 'b', 'c'), [3, 4])
element_to_search = [3, 4]
result = my_tuple.count(element_to_search)
print(result) # Output: 2
output
2
Example 4: Counting Tuples occurrences in a Tuple with count()
The count()
method can be used to count the number of occurrences of a tuple in a tuple.
In the following example, the tuple ('a', 'b')
appears once in the tuple:
my_tuple = ('a', ('a', 'b'), [3, 4], ('a', 'b', 'c'), [3, 4])
element_to_search = ('a', 'b')
result = my_tuple.count(element_to_search)
print(result) # Output: 1
output
1
Example 5: Counting Dictionary occurrences in a Tuple with count()
The count()
method can be used to count the number of occurrences of a dictionary in a tuple.
In the following example, the dictionary {1: 'one'}
appears once in the tuple:
my_tuple = ({1: 'one'}, {2: 'two'}, {1: 'one'}, {3: 'three'})
element_to_search = {1: 'one'}
result = my_tuple.count(element_to_search)
print(result) # Output: 2
output
2
Example 6: Counting Set occurrences in a Tuple with count()
The count()
method can be used to count the number of occurrences of a set in a tuple.
In the following example, the set ('a', 'b')
appears once in the tuple:
my_tuple = (set([1, 2, 3]), set([4, 5, 6]), set([1, 2, 3]), set([7, 8, 9]))
element_to_search = set([1, 2, 3])
result = my_tuple.count(element_to_search)
print(result) # Output: 2
output
2
Example 7: Count Multiple Items in a Tuple with count()
If you want to count multiple items in a tuple, you can call count()
in a loop.
The Counter
class from the collections module in Python provides a more efficient way to count the occurrences of elements in a collection, such as a tuple, compared to using the count()
method multiple times. This is because Counter
can iterate over the entire collection in a single pass, making it significantly faster for counting multiple elements than count()
in a loop.
from collections import Counter
my_tuple = ('a', 'n', 'n', 'a', 'd', 'a', 'v', 'i', 'd')
print(Counter(my_tuple))
output
Counter({'a': 3, 'n': 2, 'd': 2, 'v': 1, 'i': 1})