Python Tuple count() Method

Python Tuple Count() Method

This is a detailed tutorial of the Python Tuple count() method. Learn to find the number of occurrences of an item (element) in a given tuple with examples.

Python Tuple count()

The method tuple.count() returns the number of times a particular item or element is repeated in a given tuple. In other words, it is used to find out the number of occurrences of a given item present in a given tuple.

Syntax

The following syntax will return the occurrences of the item in tuple and store to the reference of the variable occurrences.

count() Parameters

This method takes only one argument and that is the item for which the number of occurrences you want to find out in a given tuple. As a tuple may contain items of any data type, therefore, the item to be passed in this argument could also belong to any data type.

count() Return Value

The Python Tuple count() method returns an integer value and this value is the number of times that the item passed in the argument repeats itself in the tuple on which this method is applied.

Examples

The first example illustrates the basic usage of the count() method to find the number of occurrences of an integer value & string value present in the tuple. In the second example, we’ll find the occurrences count for collection objects like Python List and another tuple present inside a tuple.

Example 1. Count Item Occurrences in a Tuple using the method count()

Here we’ve two python tuples random1 and random2. I applied the count method on both of these methods to find out the occurences of 1 and 'A' in the tuple random1 and random2 respectively.


random1 = (1, 2, 3, 1, 4, 5, 3, 1, 6, 1 )


#Count Occurrences of 1
count = random1.count(1)


print("1 repeats", count, "times.")


random2 = ('A', 'B', 'C', 'A', 'D', 'E', 'C', 'A', 'F', 'A')


#Count Occurrences of C
count = random2.count('C')


print("C repeats", count, "times.")


Then we’ve used the print statement to print the occurrences count to console.

Output.

Example 2. Counting List & Another Tuple Item Occurences in a given Tuple

Here also we’ve two tuples. The first tuple random1 contains 5 different lists as its items while the second tuple random2 contains 5 other tuples as its items. Then we’re simply applying the count method on both of these tuples to find the occurrences count of [1,2] and (5,6) respectively.


random1 = ([1,2],[3,4],[1,2],[5,6],[7,8])


#Count Occurrences of [1,2] count = random1.count([1,2])


print("[1,2] repeats", count, "times.")


random2 = ((1,2),(3,4),(1,2),(5,6),(7,8))


#Count Occurrences of (5,6)
count = random2.count((5,6))


print("(5,6) repeats", count, "times.")


Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *