How to Access Tuple in Python

In this Python tutorial, you will learn everything about tuples in Python, including multiple ways to create a tuple and access its values with practical examples and some realistic scenarios.

While working on the Python project, we needed to securely store the user’s data, such as name, age, email, etc., in a fixed order that cannot be changed. In this situation, we can store the data in a tuple in Python.

What is Tuple in Python?

A tuple is an ordered collection in Python that can store multiple values of the same or different datatypes. It is also known as a Homogeneous or Heterogeneous Collection.

Example of Homogeneous Tuple in Python

Homogeneous means a collection contains all the values of the same datatype like this,

Employees_Id = (1001, 1002, 1003, 1004, 1005)

Here, all the values are integers in the tuple, so this is a Homogeneous collection.

Example of Heterogeneous Tuple in Python

Heterogeneous collection means the collection contains all the values that can be different datatypes.

Employee_Data = ("John", 56, "john@example.com", 15000.50)

Here, all the values belong to different data types.

Because it is an immutable collection, we cannot directly change the values of tuples.

We will store the values separated by commas inside round brackets in tuples. We can access the values of the tuples by giving their index position but cannot change it.

READ:  PdfFileReader Python example

Access Tuple in Python

Here, we will learn how to access the value of tuples in Python and display their data using index positions. We will also try to create the tuples in different ways in Python.

Let me explain the scenario so you can use these approaches in correct situations. Suppose this is my data stored in the tuples like this,

user_data_1 = ("John", 56, "john@example.com")

So this is the data of our user, and I need to access the email of the user and need to get output like this,

Email : john@example.com

Let’s see how we can access the values of tuples in Python with proper syntax, practical examples, and real-based scenarios.

Accessing Tuple Value in Python by Positive Index Position

Because the tuple is an ordered collection, every value has its unique index position in sequence starting from 0.

Syntax

tuple[index_position]
  • [index_position]: The index position is an integer value that determines the position of every value in a Tuple. If you give an index value larger than the length of the tuple, then it can throw an error like this: IndexError: tuple index out of range.

Let’s execute an example of How to Access Tuple in Python by giving a positive index position.

user_data_1 = ("John", 56, "john@example.com")

email = user_data_1[2]

print("Email is:" ,email)
Accessing Tuple Value in Python by Positive Index Position

In the above example, we have a user’s data in a tuple with its name, age, and email. We needed to access the email of the user, so we gave the index position as 2 like this
email = user_data_1[2].

Extract Tuple Value by Negative Indexing

We can also use negative indexing to access the tuple value in Python. Negative indexing is also the sequence of index positions but from the last element to the first element, and it will start from -1.

READ:  How to get user IP address in Django

It is very useful in some situations. For example, suppose you want to access the last third element of the tuple. Then, you don’t need to count it from 0; you can use a negative index directly.

Syntax

tuple[-3]

Let’s understand this with a practical example of accessing a tuple in Python.

emp_id = (1001, 1002, 1003, 1004, 1005)

last_second_id = emp_id[-2]

print("Last second Id:",last_second_id)
Extract Tuple Value by Negative Indexing

In the above code, we have a tuple of the employee’s ID, and I need to access the last-second value from the tuples. So we can use negative indexing in this type of situation like we’ve used in the code last_second_id = emp_id[-2], so it will store the last second id in the last_second_id variable.

How to Access Multiple Values of Tuple in Python

To access multiple values of tuples we can use tuple slicing in Python. Slicing is used to extract multiple values from the collection by giving the start index position and the end index position.

Syntax of tuple slicing in Python

tuple[start_index, end_index +1, step_size] 
  • start_index: It will start extracting the values from the given start index
  • end_index +1: If you give only end_index, it will extract until the given end index—1. This is why we have to give +1 with the end index so it will not skip that index.
  • step_size: This is optional, so the default step is 1.

Let’s see How we can access multiple values from the tuple in Python

emp_id = (1001, 1002, 1003, 1004, 1005, 1006, 1007)

selected_id = emp_id[2: 6]

print("Selected Employee's Id: ",selected_id)
How to Access Multiple Values of Tuple in Python

In the above code, we have a tuple of employee id’s, and we need to access some id’s from the tuple. So we are using tuple slicing like this: selected_id = emp_id[2: 6] to get multiple values from the tuple in Python.

READ:  How to Create a Matrix in Python [5 Ways]

Conclusion

In this Python article, you learned how to access tuples in Python in multiple ways, like giving a positive index position and then a negative index position, and how to extract multiple values using tuple slicing in Python.

We also explain what a tuple is in Python by giving examples of the Homogeneous and Heterogeneous tuple collections.

You may like to read: