CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

PYTHON - Access Tuple Items

Python - Access Tuple Items

Access Tuple Items

The most common way to access values within a Python tuple is using indexing, We just need to specify the index of the elements we want to retrieve to the square bracket[]notation.

In Python, atupleis an immutable ordered collection of elements. β€œImmutable” means that once a tuple is created, we cannot modify or change its contents. We can use tuples to group together related data elements, similar to lists, but with the key difference that tuples are immutable, while lists are mutable.

In addition to indexing, Python provides various other ways to access tuple items such as slicing, negative indexing, extracting a subtuple from a tuple etc. Let us go through this one-by-one βˆ’

Accessing Tuple Items with Indexing

Each element in a tuple corresponds to an index. The index starts from 0 for the first element and increments by one for each subsequent element. Index of the last item in the tuple is always β€œlength-1”, where β€œlength” represents the total number of items in the tuple. To access the elements of a tuple we just need to specify the index of the item we need to access/retrieve, as shown below βˆ’

PYTHON CODE
tuple[3]

Example

Following is the basic example to access tuple items with slicing index βˆ’

PYTHON CODE
tuple1 = ("Rohan", "Physics", 21, 69.75)
tuple2 = (1, 2, 3, 4, 5)

print ("Item at 0th index in tuple1: ", tuple1[0])
print ("Item at index 2 in tuple2: ", tuple2[2])

It will produce the following output βˆ’

PYTHON CODE
Item at 0th index in tuple1:  Rohan
Item at index 2 in tuple2:  3

Accessing Tuple Items with Negative Indexing

Negative indexing in Python is used to access elements from the end of a tuple, with-1referring to the last element,-2to the second last, and so on.

We can also access tuple items with negative indexing by using negative integers to represent positions from the end of the tuple.

Example

In the following example, we are accessing tuple items with negative indexing βˆ’

PYTHON CODE
tup1 = ("a", "b", "c", "d")
tup2 = (25.50, True, -55, 1+2j)

print ("Item at 0th index in tup1: ", tup1[-1])
print ("Item at index 2 in tup2: ", tup2[-3])

We get the output as shown below βˆ’

PYTHON CODE
Item at 0th index in tup1:  d
Item at index 2 in tup2:  True

Accessing Range of Tuple Items with Negative Indexing

By range of tuple items, we mean accessing a subset of elements from a tuple using slicing. Therefore, we can access a range of tuple items with negative indexing by using the slicing operation in Python.

Example

In the example below, we are accessing a range of tuple items by using negative indexing βˆ’

PYTHON CODE
tup1 = ("a", "b", "c", "d")
tup2 = (1, 2, 3, 4, 5)

print ("Items from index 1 to last in tup1: ", tup1[1:])
print ("Items from index 2 to last in tup2", tup2[2:-1])

It will produce the following output βˆ’

PYTHON CODE
Items from index 1 to last in tup1: ('b', 'c', 'd')
Items from index 2 to last in tup2: (3, 4)

Access Tuple Items with Slice Operator

The slice operator in Python is used to fetch one or more items from the tuple. We can access tuple items with the slice operator by specifying the range of indices we want to extract. It uses the following syntax βˆ’

PYTHON CODE
[start:stop]

Where,

  • startis the starting index (inclusive).
  • stopis the ending index (exclusive).

Example

In the following example, we are retrieving subtuple from index 1 to last in β€œtuple1” and index 0 to 1 in β€œtuple2”, and retrieving all elements in β€œtuple3” βˆ’

PYTHON CODE
tuple1 = ("a", "b", "c", "d")
tuple2 = (25.50, True, -55, 1+2j)
tuple3 = (1, 2, 3, 4, 5)
tuple4 = ("Rohan", "Physics", 21, 69.75)

print ("Items from index 1 to last in tuple1: ", tuple1[1:])
print ("Items from index 0 to 1 in tuple2: ", tuple2[:2])
print ("Items from index 0 to index last in tuple3", tuple3[:])

Following is the output of the above code βˆ’

PYTHON CODE
Items from index 1 to last in tuple1:  ('b', 'c', 'd')
Items from index 0 to 1 in tuple2:  (25.5, True)
Items from index 0 to index last in tuple3 ('Rohan', 'Physics', 21, 69.75)

Accessing Sub Tuple from a Tuple

A subtuple is a part of a tuple that consists of a consecutive sequence of elements from the original tuple.

We can access a subtuple from a tuple by using the slice operator with appropriate start and stop indices. It uses the following syntax βˆ’

PYTHON CODE
my_tuple[start:stop]

Where,

  • startis the starting index (inclusive).
  • stopis the ending index (exclusive) of the subtuple.

If we does not provide any indices, the slice operator defaults to starting from index 0 and stopping at the last item in the tuple.

Example

In this example, we are fetching subtuple from index β€œ1 to 2” in β€œtuple1” and index β€œ0 to 1” in β€œtuple2” using slice operator βˆ’

PYTHON CODE
tuple1 = ("a", "b", "c", "d")
tuple2 = (25.50, True, -55, 1+2j)

print ("Items from index 1 to 2 in tuple1: ", tuple1[1:3])
print ("Items from index 0 to 1 in tuple2: ", tuple2[0:2])

The output obtained is as follows βˆ’

PYTHON CODE
Items from index 1 to 2 in tuple1:  ('b', 'c')
Items from index 0 to 1 in tuple2:  (25.5, True)