Python Tuple and its Methods with Examples

A tuple is Python’s built-in immutable sequence that just looks like a list and uses parenthesis instead of square brackets. It supports indexing and only has two methods called: count( ) and index( ).

Python Tuple and its Methods with Examples

 

Accessing tuple items

Accessing items in the tuple is similar to like in the list, just specify the index position of the item.

# define tuple in two ways

# first way
# using tuple constructor
tuple1 = tuple(("Python", "Java", "C", "C++"))

# second way by direct parenthesis
tuple1 = ("Python", "Java", "C", "C++") #overrides the tuple1 object

# accessing items with index position
print(tuple1[0])
print(tuple1[1])
print(tuple1[2])
print(tuple1[3])

Output:

Python
Java
C
C++

 

Immutable object

Once you define a tuple object, you cannot change and append new items into it. That is why it is called an immutable data type.

Python Tuple is mostly used to define the collection of items that are fixed and rarely changed.

Let’s take the example of seven continents in the world and arrange them in the collection.

In this case, the tuple will be the best choice than the list as it holds the constant values that rarely change.

# tuple of seven continents
my_tuple = ("Asia", "Africa", "North America",
            "South America", "Antarctica", "Europe", "Australia")

If there is a case, you want to exclude the Antarctica continent as there is no human habitation. Then you have to redefine the tuple object as it does not support remove( ) or pop( ) methods as in a Python list.

# excluding Antarctica
my_tuple = ("Asia", "Africa", "North America",
            "South America", "Europe", "Australia")

 

Duplicate items

Python Tuple can also support duplicate items like a list. You can define the same item multiple times in the tuple having different index positions.

# lets repeat Europe three times
my_tuple = ("Asia", "Europe", "North America",
            "South America", "Europe", "Europe", "Australia")

print(my_tuple)

# check type of tuple object
print(type(my_tuple))

Output:

('Asia', 'Europe', 'North America', 'South America', 'Europe', 'Europe', 'Australia')

<class 'tuple'>

 

Tuple Methods

There are only two built-in methods that tuple supported because of their immutable nature.

1.  count( )

This method returns the number of times a particular item occurred in the tuple.

my_tuple = ("Asia", "Europe", "North America",
            "South America", "Europe", "Europe", "Australia")

# count Europe in my_tuple
count = my_tuple.count("Europe")

print(f"Europe occurred {count} times in my_tuple")

Output:

Europe occurred 3 times in my_tuple

 

2. index( )

This method returns the index position of the item located inside the tuple object.

tuple2 = ("Python", "Java", "C", "C++")

# index position of Python
print(tuple2.index("Python"))

# index position of C++
print(tuple2.index("C++"))

Output:

0
3

It raises ValueError if the specified item pass as an argument is not present.

tuple2 = ("Python", "Java", "C", "C++")

# find index position of Swift
# returns ValueError
print(tuple2.index("Swift"))

Output:

Traceback (most recent call last):
    print(tuple2.index("Swift"))
ValueError: tuple.index(x): x not in tuple

Leave a Comment