Get The Most Affordable Hosting in the World!

Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.

Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!

Get Started Now!

Since a tuple in Python is immutable, the tuple class doesn't define methods for adding or removing items. The tuple class defines only two methods.

Sr.No Methods & Description
1

tuple.count(obj)

Returns count of how many times obj occurs in tuple

2 tuple.index(obj)

Returns the lowest index in tuple that obj appears

Finding the Index of a Tuple Item

The index() method of tuple class returns the index of first occurrence of the given item.

Syntax

tuple.index(obj)

Return value

The index() method returns an integer, representing the index of the first occurrence of "obj".

Example

Take a look at the following example −

 
tup1 = (25, 12, 10, -21, 10, 100) print ("Tup1:", tup1) x = tup1.index(10) print ("First index of 10:", x)

It will produce the following output −

Tup1: (25, 12, 10, -21, 10, 100)
First index of 10: 2

Counting Tuple Items

The count() method in tuple class returns the number of times a given object occurs in the tuple.

Syntax

tuple.count(obj)

Return Value

Number of occurrence of the object. The count() method returns an integer.

Example

 
tup1 = (10, 20, 45, 10, 30, 10, 55) print ("Tup1:", tup1) c = tup1.count(10) print ("count of 10:", c)

It will produce the following output −

Tup1: (10, 20, 45, 10, 30, 10, 55)
count of 10: 3

Example

Even if the items in the tuple contain expressions, they will be evaluated to obtain the count.

Tup1 = (10, 20/80, 0.25, 10/40, 30, 10, 55) print ("Tup1:", tup1) c = tup1.count(0.25) print ("count of 10:", c)

It will produce the following output −

Tup1: (10, 0.25, 0.25, 0.25, 30, 10, 55)
count of 10: 3




The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.
Hjalp dette svar dig? 1 Kunder som kunne bruge dette svar (1 Stem)