Python - Tuple Methods



Tuple is one of the fundamental data structures in Python, and it is an immutable sequences. Unlike lists, tuples cannot be modified after creation, making them ideal for representing fixed collections of data. This immutability play a crucial role in various scenarios where data stability and security are important. It can contain elements of different data types, such as integers, floats, strings, or even other tuples.

Python Tuple Methods

The tuple class provides few methods to analyze the data or elements. These methods allows users to retrieve information about the occurrences of specific items within a tuple and their respective indices. Since it is immutable, this class doesn't define methods for adding or removing items. It defines only two methods and these methods provide a convenient way to analyze tuple data.

Listing All the Tuple Methods

To explore all available methods for tuples, you can utilize the Python dir() function, which lists all properties and functions related to a class. Additionally, the help() function provides detailed documentation for each method. Here's an exampl:

print(dir((1, 2)))
print(help((1, 2).index))

The above code snippet provides a complete list of properties and functions related to the tuple class. It also demonstrates how to access detailed documentation for a specific method in your Python environment. Here is the output −

['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
Help on built-in function index:

index(value, start=0, stop=9223372036854775807, /) method of builtins.tuple inst ance
    Return first index of value.
    
    Raises ValueError if the value is not present.
(END)

Below are the built-in methods for tuples. Let's explore each method's basic functionality −

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
Advertisements