Python Tuple Methods


Method Description
count() To count the number of times a value appears in a tuple.
index() To find the index of any item.

Python Tuple count() Method

To count the number of times a value appears in a tuple.

In [1]:
emp_details = ('Tim','Smith','UK',28,True,'UK')
emp_details.count('UK')
Out[1]:
2

Python Tuple index() Method

To find the index of any item.

In [1]:
emp_details = ('Tim','Smith','UK',28,True)
In [2]:
emp_details.index('Smith')
Out[2]:
1

It returns an error if the index is not found in the tuple.

In [3]:
emp_details.index('Tim5')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-3-804d1044b99a> in <module>()
----> 1 emp_details.index('Tim5')

ValueError: tuple.index(x): x not in tuple