Accessing Elements of a Tuple | Tuple

Course Content

Python Data Structures

Accessing Elements of a Tuple

Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0, just like with lists.

Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1, the second to last is -2, and so on.

In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).

Given the tuple:

Retrieve the first, second, fifth, and seventh items to create a new tuple:

Everything was clear?

Section 3. Chapter 2

Course Content

Python Data Structures

Accessing Elements of a Tuple

Accessing elements in a tuple is straightforward and works the same way as with lists. Simply specify the index number inside square brackets. Remember, indexing starts at 0, just like with lists.

Tuples also support negative indexing, where the indexing begins from the end. Thus, the last element has an index of -1, the second to last is -2, and so on.

In the example above, we access the last element (indexed at -1) and the fourth element from the end (indexed at -4).