Python Access Tuple Element Tutorial

In this section, we will learn how to access the elements of a tuple in Python.

Note: in this section, we’re assuming you’re familiar with the Python Tuple object and Python list object.

How to access the elements of a tuple object in Python?

The elements in a tuple object have index numbers. For example, the first element of a tuple object has the index number 0, the second element has the index number 1 and so on…

Now using this index number and a pair of brackets `[]` we can access any element within a tuple object as the syntax below shows how to:

Access Tuple Syntax:

tupleObject[index-number]

`tupleObject`: this is the tuple object that we want to access one of its elements.

`[index-number]`: we use a pair of brackets after the name of a tuple object and within that pair we put the index number of the element that want to read.

For example, if we want to access the first element of a tuple object named `tup1` then here’s how to do so:

tup1[0]

Access Tuple Example

tup1 = ("Ellen","Jack", "Omid","Elon")

print(tup1[0])

print(tup1[1])

print(tup1[2])

Output:

Ellen

Jack

Omid

Python Tuple: Negative Indexing

The value we put as the index number within the body of a pair of brackets could be also a negative value!

For tuples, the value -1 points to the last element of a tuple object, the value -2 points to the element before the last element of a tuple and so on…

Example: using negative index number to access elements in python Tuple

tup1 = ("Ellen","Jack", "Omid","Elon")

print(tup1[-1])

print(tup1[-2])

print(tup1[-3])

Output:

Elon

Omid

Jack

Python find Element in Tuple

There are multiple ways by which we can check a tuple object to see if it has a specific value as part of its element or not. One of the simplest ways to do this is by using the `in` or `not in` operators with the combination of a conditional statements like `if` for example.

Example: finding elements in a tuple object

tup1 = ("Ellen","Jack", "Omid","Elon")

if "Ellen" in tup1:
    print("Yes, the target tuple object has the value Ellen as one of its elements")

Output:

Yes, the target tuple object has the value Ellen as one of its elements

Replace Element in Tuple Python

Tuples are immutable! Meaning we can’t change their elements (remove/ replace or add a new one). But one thing we can do is to convert a tuple into another container data type (like List) that we’re allowed to modify the elements in there and then convert back the modified elements into a tuple object.

Example: Python Tuple Replace Value

tuple1 = (1,2,3,4,5,6)

list1 = list(tuple1)

list1[0] = 1000

tuple1 = tuple(list1)

print(tuple1)

Output:

(1000, 2, 3, 4, 5, 6)

As you can see, we first called the `list()` function and created a list object from the target tuple object. Then in that list applied the necessary modifications and finally created a tuple object from the list using the `tuple()` function.

More to Read:

Python Tuple Slice

Leave a Reply