Python Tuple Methods



1) Python Tuple count() Method

The count() method to determine the number of times the given item appears in the tuple.


Syntax

tuple.count(item)

Example

myTuple = ('Python', 'HTML', 'CSS', 'HTML', 'Angular', 'HTML')

print(myTuple.count('HTML'))

Output

3

2) Python Tuple index() Method

The index() method searches for the first occurrence of the given item and returns its index. If a defined item is not found, it raises the ‘ValueError’ exception.

The optional arguments start and end limit the search to a particular subsequence of the tuple.



Syntax

tuple.index(item,start,end)

Example

myTuple = ('Python', 'HTML', 'CSS', 'HTML', 'Angular', 'HTML')

print(myTuple.index('HTML'))

Output

1

Limit index() Search to Subsequence

We want to search the tuple from the middle, define the start parameter.


Example

myTuple = ('Python', 'HTML', 'CSS', 'HTML', 'Angular', 'HTML')

print(myTuple.index('HTML', 2))


myTuple = ('Python', 'HTML', 'CSS', 'HTML', 'Angular', 'HTML')

print(myTuple.index('HTML', 2, 5))

Output

3
3