Python | Count occurrences of an element in a Tuple

Python | Count occurrences of an element in a Tuple

To count occurrences of an element in a tuple, you can use the count() method of the tuple. Here's how you can do it:

# Define the tuple tup = (1, 2, 3, 2, 4, 2, 5) # Count occurrences of the number 2 count_2 = tup.count(2) print(f"Number 2 appears {count_2} times in the tuple.") 

In this example, the output will be:

Number 2 appears 3 times in the tuple. 

This indicates that the number 2 appears three times in the tuple tup.


More Tags

visualization cdo-climate mkdir html5-audio nosql roles intervention cloudera-cdh .net-standard django-template-filters

More Programming Guides

Other Guides

More Programming Examples