Python Access Tuple | Explained

In Python, the term tuple refers to an ordered collection of objects that cannot be modified. Tuple and list data structures are very similar; the only difference is that tuple data can not be modified, unlike lists. Tuples are initialized inside the parentheses “( )”, and the list is initialized inside the square bracket ”[ ]”.

Tuple value is created by putting single or double quotation marks and is placed inside the parentheses by following separated comma syntax. The items of the tuple are accessed or called using their index number.

In this article, the Python access tuples will be discussed in detail with several examples. The following outcomes are explained in this article:

So let’s get started!

How to Access Tuples in Python?

The index number method is used in various forms to access items of Python tuples. Using the index number, the specific items of the tuple or the tuple items within a specific range can be accessed. Specify a positive number inside the square bracket to access the item of a tuple, such as “Tuple[index number]”. The negative index number accessed the items in the tuple by starting from the last index. For instance, “Tuple[-1]” will be used to access the last index of a tuple.

Example 1: Accessing Tuple Items Using Positive and Negative Indexing

In the following code, the tuple items are accessed using positive and negative index numbers.

Code:

tuple_value = ("Alex", "John", "David",5)
print('Tuple Value at Index 0 is:',tuple_value[0])
print('Tuple Value at Index 1 is:',tuple_value[1])

#Negative Index indicate the end value
print('\nTuple Value at Negative Index -1 is:',tuple_value[-1])
print('Tuple Value at Negative Index -4 is:',tuple_value[-4])

In the above code example:

  • The tuple is initialized with multiple values and stored in a variable named “tuple_value”.
  • To access items of a tuple, the index number of that specific item is passed inside the square bracket of variable names like “tuple_value[0]”.
  • The negative index indicates that the index number begins from the end index number of the tuple. The “-1” index value will access the last item of the tuple, i.e., “5”. Similarly, the “-4” index value will access the fourth last item of the tuple, i.e., “Alex” in our case.

Output:

In the above output, the items of the tuple are accessed using their positive and negative index number.

Example 2: Accessing Tuple Items Using Range of Indexes

You can access multiple items of a tuple between a specific range by specifying the initial and final indexes:

Code:

tuple_value = ("Alex", "John", "David",5 ,10, "Lily", "Mary")

#returned the items between the specified range
print(tuple_value[3:6])

#returned the range from start
print(tuple_value[:4])

#returned the range till end
print(tuple_value[2:])

In the above mentioned code:

  • The “tuple_value” containing number and string is initialized in the program.
  • The range numbers are specified inside the bracket using the index’s start number and the index’s end number, such as “[3:6]”. Here the items of the tuple are accessed from the index number “3” to index number “6”. The index number “6” is not included while accessing the items of the tuple.
  • To access items of a tuple from the beginning to the specific index number, the statement is used like this “[ :4]”. This will access the first three items of the tuple.
  • To access items from the specific index number till the last index, use a statement like this “[2: ]”.

Output:

In the above output, the tuple items are accessed using various ranges of index numbers.

Example 3: Checking if Particular Items Exist in Tuple

In the below code example, the if-else statements are used to check the existence of particular items:

Code:

tuple_value = ("Alex", "John", "David","Lily", 5)
if "John" in tuple_value:
  print("Yes, 'John' is the items of tuple")

elif 5 in tuple_value:
  print("Number 5 is present in Tuple items")

In the mentioned code:

  • A variable tuple_value is initialized at the start of the program.
  • The “if” statement checks the existence of an item “John” in the tuple.
  • The “elif” statement checks the existence of an item “5” in the tuple.

Output:

The above output verifies that the item value “John” is present in the tuple.

That’s all from this Python guide!

Conclusion

In Python, the index number method is used to access the items of the Python tuple. The items of tuples are accessed individually and in the form of ranges. Specify a positive number inside the square bracket to access the item of a tuple, such as “Tuple[index number]”. The negative index number accessed the items in the tuple starting from the last index. The range of tuple items can be accessed by mentioning the start and end index numbers. This article provided a complete guide on accessing the tuple items in Python.