Python Tuple Methods

Python Tuples is an immutable collection of that are more like lists.
Python Provides a couple of methods to work with tuples.
In this article, we will discuss these two methods in detail with the help of some examples.

MethodDescription
count()The count() method of Tuple returns the number of times the given element appears in the tuple.
index()The Index() method returns the first occurrence of the given element from the tuple.

Count()

The count() method of Tuple returns the number of times the given element appears in the tuple.
count() is a Python built-in function that returns the number of times an object appears in a list.
The count() method is one of Python's built-in functions.
It returns the number of times a given value occurs in a string or a list, as the name implies.
Example


a=(5,3,4,5,7,2,5,7,4,2,5,4,5,)
b=a.count(4)
print(b)          #Output is 3

Index()

The index() method finds the first occurrence of the specified value.
The index() method raises an exception if the value is not found.
The tuple index() method helps us to find the index or occurrence of an element in a tuple.
This function basically performs two functions: Giving the first occurrence of an element in the tuple.
Raising an exception if the element mentioned is not found in the tuple.
Example

a=(1,2,3,4,2,3,4,2,4,5,6,4,4,5,6)
b=a.index(4)
print(b)         #Output is 3