Python Tutorials
Basic

Data Structure and Data Types

String

List

Tuple

Set

Dictionary

Functions / Methods

Advance

Exception Handling

Python Access Tuple Items


Access Tuple Items

The tuple items are indexed. The first item of the tuple index position is 0. You can access tuple items by using the index position with square brackets ([]).

Example
colors_tuple = ("Red", "Blue", "Green")
print(colors_tuple[1])
Blue

In the above example, we declared a colors tuple (colors_tuple) and added some color names to the tuple. Using the index position, we are currently accessing the first tuple item ([1]). The first index position item in this tuple is "Blue".


Negative Indexing in Python

When you use negative indexing in Python, item picking starts from the end of the tuple.

You can access items from the end, meaning that if you put index [-1], you get the last item from the tuple, and if you put index [-2], you get the second-last item from the tuple.

Example
colors_tuple = ("Red", "Blue", "Green")
print(colors_tuple[-1])
Green

In the above example, we use negative indexing to access the last item ([-1]) from the tuple. In this case, "Green" is the last item on the tuple.

You can access the second-last ("Blue") item of the tuple by using negative indexing [-2].


Range of Indexes

Range of Indexes is also called Slicing.

In the slicing, you can specify where to start and where to end, and when you do that, it returns a new tuple with items between the start and end indexes.

Example
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[1:5])
('Blue', 'Green', 'Orange', 'White')

python-tuple-slicing

In the above example, we set the start index range from 1, and the end index is 5, resulting in a new tuple with the following items (1) "Blue", (2) "Green", (3) "Orange", (4) "White". The output includes the index position 1 ("Blue") item but excludes the index position 5 ("Black") item.

Whenever you perform a slicing, it includes the start position item and excludes the end position item. Remember that the index position of the first item is 0.


Range of Indexes Specifies Only the Start Position

In the slicing, when you specify where to start and ignore the end item position, automatically it goes to the end of the tuple.

Example
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[3:])
('Orange', 'White', 'Black', 'Yellow')

python-tuple-slicing

In the above example, we specify the start item position (3) and ignore the end item position so that the new tuple will cover all items after the start item index from the original tuple (("Orange", "White", "Black", "Yellow")).


Range of Indexes Specifies Only the End Position

In the slicing, when you ignore where to start and specify only the end item position, the indexes will begin from the first item and goes up to the specified index position.

Example
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[:4])
('Red', 'Blue', 'Green', 'Orange')

python-tuple-slicing

In the above example, we ignore the start item position and specify the end item position (4) so that the new tuple will cover all items before the end item index from the original tuple (("Red", "Blue", "Green", "Orange")).


Range of Negative Index

In the slicing with negative indexes, you can specify where to start and where to end (negative indexing starts picking items from the end), and when you do that, it returns a new tuple with items between the start and end indexes.

Example
colors_tuple = ("Red", "Blue", "Green", "Orange", "White", "Black", "Yellow")
print(colors_tuple[-5:-2])
('Green', 'Orange', 'White')

python-tuple-slicing-with-negative-indexing

In the above example, the negative index position for the start item is -5, and the end index position is -2, resulting in a new tuple with the following items (-5) "Green", (-4) "Orange", (-3) "White". The output includes the index position -5 ("Green") item but excludes the index position -2 ("Black") item.


Check item exists in the tuple

Check if the specified item exists in the tuple by using the "in" keyword.

Example
colors_tuple = ("Red", "Blue", "Green")
if "Blue" in colors_tuple:
    print("Blue exists in the tuple.")
else:
    print("Blue does not exist in the tuple.")
Blue exists in the tuple.

In the above example, we use the "in" keyword to check whether the "Blue" color exists in the tuple. Here, the "Blue" color exists in the tuple, so the output statement is "Blue exists in the tuple.".