Skip to content

Access the Tuple

Section titled “Navigate Tuple Items: A Guide to Access the Tuple List”

In Python, Tuple is an immutable sequence of elements. It means that once a tuple is created, we cannot change its values. We can access the tuple elements using the index number. We can also access the tuple elements using the negative index number. We can also access the tuple elements using the range of index numbers. We can also access the tuple elements using the range of negative index numbers.

In Python, we can access the tuple elements using the index number. The index number starts from 0. We can also access the tuple elements using the negative index number. The negative index number starts from -1. We can also access the tuple elements using the range of index numbers. We can also access the tuple elements using the range of negative index numbers.

Access the Tuple Elements Using the Index Number

Section titled “Access the Tuple Elements Using the Index Number”

In Python, we can access the tuple elements using the index number. The index number starts from 0. We can access the tuple elements using the index number by using the following syntax.

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0])
print(data[1])
print(data[2])
print(data[3])

Output:

command
C:\Users\username>python tuple_index.py
a
b
c
d

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the index number. The output shows that the tuple elements are accessed using the index number.

Access the Tuple Elements Using the Negative Index Number

Section titled “Access the Tuple Elements Using the Negative Index Number”

In Python, we can access the tuple elements using the negative index number. The negative index number starts from -1. We can access the tuple elements using the negative index number by using the following syntax.

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[-1])
print(data[-2])
print(data[-3])
print(data[-4])

Output:

command
C:\Users\username>python tuple_index.py
j
i
h
g

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the negative index number. The output shows that the tuple elements are accessed using the negative index number.

The following diagram illustrates the indexing of a tuple with five elements:

tuple_indexing.py
my_list = ('a', 'b', 'c', 'd', 'e')
Element‘a’‘b’‘c’‘d’‘e’
Index01234
Index (Negative)-5-4-3-2-1

In Python, Multidimensional Tuples are tuples that contain other tuples. We can access the tuple elements using the index number. We can also access the tuple elements using the negative index number. We can also access the tuple elements using the range of index numbers. We can also access the tuple elements using the range of negative index numbers.

Example:

multidimensional_tuple.py
data = (('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'))
print(data[0])
print(data[1])
print(data[2])

Output:

command
C:\Users\username>python multidimensional_tuple.py
('a', 'b', 'c')
('d', 'e', 'f')
('g', 'h', 'i')

In this example, we declare a multidimensional tuple and assign it to the variable data. We then print the tuple elements using the index number. The output shows that the tuple elements are accessed using the index number.

Accessing Elements of Multidimensional Tuples

Section titled “Accessing Elements of Multidimensional Tuples”

You can access the elements of a multidimensional tuple by using the index number. The index number starts from 0. We can also access the tuple elements using the negative index number. The negative index number starts from -1. We can also access the tuple elements using the range of index numbers. We can also access the tuple elements using the range of negative index numbers.

Example:

multidimensional_tuple.py
data = (('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'))
print(data[0][0])
print(data[1][1])
print(data[2][2])

Output:

command
C:\Users\username>python multidimensional_tuple.py
a
e
i

In this example, we declare a multidimensional tuple and assign it to the variable data. We then print the tuple elements using the index number. The output shows that the tuple elements are accessed using the index number.

Index012
0‘a’‘b’‘c’
1‘d’‘e’‘f’
2‘g’‘h’‘i’

You can also use negative indices to access multidimensional lists. For example, the last element of the last list can be accessed using an index of -1 for the first dimension and an index of -1 for the second dimension.

Index-3-2-1
-3‘a’‘b’‘c’
-2‘d’‘e’‘f’
-1‘g’‘h’‘i’

Access the Tuple Elements Using the Range of Index Numbers

Section titled “Access the Tuple Elements Using the Range of Index Numbers”

In Python, we can access the tuple elements using the range of index numbers. We can access the tuple elements using the range of index numbers by using the following syntax.

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0:4])
print(data[1:5])
print(data[2:6])
print(data[3:7])

Output:

command
C:\Users\username>python tuple_index.py
('a', 'b', 'c', 'd')
('b', 'c', 'd', 'e')
('c', 'd', 'e', 'f')
('d', 'e', 'f', 'g')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of index numbers. The output shows that the tuple elements are accessed using the range of index numbers.

tuple slices are a way of accessing a subset of a tuple. In Python, tuple slices are created using the syntax tuple[start:stop], where start is the index of the first element to include in the slice and stop is the index of the first element to exclude from the slice. If start is omitted, it defaults to 0, and if stop is omitted, it defaults to the length of the tuple.

Syntax:

syntax.py
tuple[start:stop]

Access the Tuple Elements Using the Range of Negative Index Numbers

Section titled “Access the Tuple Elements Using the Range of Negative Index Numbers”

In Python, we can access the tuple elements using the range of negative index numbers. We can access the tuple elements using the range of negative index numbers by using the following syntax.

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[-4:-1])
print(data[-5:-2])
print(data[-6:-3])
print(data[-7:-4])

Output:

command
C:\Users\username>python tuple_index.py
('g', 'h', 'i')
('f', 'g', 'h')
('e', 'f', 'g')
('d', 'e', 'f')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers. The output shows that the tuple elements are accessed using the range of negative index numbers.

Access the Tuple Elements Using the Range of Index Numbers and Negative Index Numbers

Section titled “Access the Tuple Elements Using the Range of Index Numbers and Negative Index Numbers”

In Python, we can access the tuple elements using the range of index numbers and negative index numbers. We can access the tuple elements using the range of index numbers and negative index numbers by using the following syntax.

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0:-1])
print(data[1:-2])
print(data[2:-3])
print(data[3:-4])

Output:

command
C:\Users\username>python tuple_index.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i')
('b', 'c', 'd', 'e', 'f', 'g', 'h')
('c', 'd', 'e', 'f', 'g')
('d', 'e', 'f')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of index numbers and negative index numbers. The output shows that the tuple elements are accessed using the range of index numbers and negative index numbers.

In Python, we can access the tuple elements using the range of negative index numbers and index numbers and step. We can access the tuple elements using the range of negative index numbers and index numbers and step by using the following syntax.

syntax.py
tuple[start:stop:step]

Example:

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0:-1:2])
print(data[1:-2:2])
print(data[2:-3:2])
print(data[3:-4:2])

Output:

command
C:\Users\username>python tuple_index.py
('a', 'c', 'e', 'g', 'i')
('b', 'd', 'f', 'h')
('c', 'e', 'g')
('d', 'f')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers and index numbers and step. The output shows that the tuple elements are accessed using the range of negative index numbers and index numbers and step.

In Python, we can omit the start and stop indices. We can omit the start and stop indices by using the following syntax.

syntax.py
tuple[start:]
tuple[:stop]

Example:

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0:])
print(data[:5])
print(data[2:])
print(data[:7])

Output:

command
C:\Users\username>python tuple_index.py
('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
('a', 'b', 'c', 'd', 'e')
('c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
('a', 'b', 'c', 'd', 'e', 'f', 'g')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers and index numbers and step. The output shows that the tuple elements are accessed using the range of negative index numbers and index numbers and step.

In Slicing, when you omit the start index, it defaults to 0. When you omit the stop index, it defaults to the length of the tuple.

data[0:] -> data[0:len(data)] data[:5] -> data[0:5] data[2:] -> data[2:len(data)] data[:7] -> data[0:7]

Omitting the Start and Stop Indices with Step

Section titled “Omitting the Start and Stop Indices with Step”

In Python, we can omit the start and stop indices with step. We can omit the start and stop indices with step by using the following syntax.

syntax.py
tuple[start::step]
tuple[:stop:step]

Example:

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0::2])
print(data[:5:2])
print(data[2::2])
print(data[:7:2])

Output:

command
C:\Users\username>python tuple_index.py
('a', 'c', 'e', 'g', 'i')
('a', 'c', 'e')
('c', 'e', 'g', 'i')
('a', 'c', 'e', 'g')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers and index numbers and step. The output shows that the tuple elements are accessed using the range of negative index numbers and index numbers and step.

In Slicing, when you omit the start index, it defaults to 0. When you omit the stop index, it defaults to the length of the tuple.

data[0::2] -> data[0:len(data):2] data[:5:2] -> data[0:5:2] data[2::2] -> data[2:len(data):2] data[:7:2] -> data[0:7:2]

Omitting the Start and Stop Indices with Negative Step

Section titled “Omitting the Start and Stop Indices with Negative Step”

In Python, we can omit the start and stop indices with negative step. We can omit the start and stop indices with negative step by using the following syntax.

syntax.py
tuple[start::-step]
tuple[:stop:-step]

Example:

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0::-2])
print(data[:5:-2])
print(data[2::-2])
print(data[:7:-2])

Output:

command
C:\Users\username>python tuple_index.py
('a',)
('j', 'h', 'f')
('c', 'a')
('j', 'h', 'f', 'd')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers and index numbers and step. The output shows that the tuple elements are accessed using the range of negative index numbers and index numbers and step.

data[0::-2] -> data[0: len(data): -2] data[:5:-2] -> data[0:5: -2] data[2::-2] -> data[2: len(data): -2] data[:7:-2] -> data[0:7: -2]

In Python, we can omit the both indices. We can omit the both indices by using the following syntax.

syntax.py
tuple[::step]

Example:

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[::2])
print(data[::3])
print(data[::4])
print(data[::5])

Output:

command
C:\Users\username>python tuple_index.py
('a', 'c', 'e', 'g', 'i')
('a', 'd', 'g', 'j')
('a', 'e', 'i')
('a', 'f')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers and index numbers and step. The output shows that the tuple elements are accessed using the range of negative index numbers and index numbers and step.

data[::2] -> data[0: len(data): 2] data[::3] -> data[0: len(data): 3] data[::4] -> data[0: len(data): 4] data[::5] -> data[0: len(data): 5]

In Python, we can slice with negative indices. We can slice with negative indices by using the following syntax.

syntax.py
tuple[start:stop:step]

Example:

tuple_index.py
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[-1::-2])
print(data[-2::-2])
print(data[:-3:-2])
print(data[-4::-2])

Output:

command
C:\Users\username>python tuple_index.py
('j', 'h', 'f', 'd', 'b')
('i', 'g', 'e', 'c', 'a')
('j',)
('g', 'e', 'c', 'a')

In this example, we declare a tuple and assign it to the variable data. We then print the tuple elements using the range of negative index numbers and index numbers and step. The output shows that the tuple elements are accessed using the range of negative index numbers and index numbers and step.

data[-1::-2] -> data[-1: len(data): -2] data[-2::-2] -> data[-2: len(data): -2] data[:-3:-2] -> data[0: -3: -2] data[-4::-2] -> data[-4: len(data): -2]

t[1] and t[1:2] look alike and behave nothing alike. One reaches inside the tuple and hands you an element; the other builds a new tuple. Their failure modes differ too — one raises, the other quietly returns empty.

diagram Diagram mermaid
index_vs_slice.py
t = (10, 20, 30, 40, 50)
 
print(t[0], type(t[0]).__name__)       # 10 int
print(t[1:3], type(t[1:3]).__name__)   # (20, 30) tuple
 
print(t[99:])                          # ()  <- silent
t[99]                                  # IndexError: tuple index out of range
no_copy.py
t = (10, 20, 30)
print(t[:] is t)          # True   <- the very same object
 
nums = [10, 20, 30]
print(nums[:] is nums)    # False  <- a real copy

A tuple cannot change, so handing back the original is indistinguishable from handing back a copy — and cheaper. A list can change, so [:] must build a new one. This is why t[:] is not a way to “defensively copy” a tuple: there is nothing to defend against, and you did not get a new object.

Drag the start and stop handles. Watch which cells the slice selects, what happens when a bound runs past the end, and how negative numbers count from the right.

sketch Slicing a tuple p5.js
Slice bounds are clamped to the tuple, never raise, and always return a tuple. Negative bounds count from the right end.
unpacking.py
t = (10, 20, 30, 40, 50)
 
a, b, c, d, e = t              # exact count required
first, *mid, last = t
print(first, mid, last)        # 10 [20, 30, 40] 50
print(type(mid).__name__)      # list   <- NOT a tuple
 
x, y = t                       # ValueError: too many values to unpack (expected 2, got 5)

The starred target always collects into a list, even when the source is a tuple. If you need a tuple back, wrap it: tuple(mid).

nested.py
nest = (1, [2, 3], 4)
nest[1].append(99)
print(nest)              # (1, [2, 3, 99], 4)   <- allowed
 
nest[0] = 5              # TypeError: 'tuple' object does not support item assignment
hash((1, [2], 3))        # TypeError: unhashable type: 'list'

The tuple stores references. It refuses to swap one for another, but it has no say over what those objects do internally. That is also why the tuple stops being hashable the moment it contains a list — so it cannot be a dict key.

A tuple has exactly two methods, count and index. Everything else on a list is a mutator, and a tuple has nothing to mutate.

pch.quizTag pch.quizDefaultTitle
  1. For a tuple t, what is t[:] is t?

    pch.quizShowAnswer

    B — True, because a tuple cannot change so returning the original is safe — Slicing a whole tuple returns the same object. For a list, nums[:] is nums is False, because a list can be mutated and so a real copy is required.

  2. t = (10, 20, 30). What do t[99] and t[99:] do?

    pch.quizShowAnswer

    C — t[99] raises IndexError, t[99:] returns () with no error — Indexing must produce an element, so an out-of-range index raises. Slicing clamps its bounds to the sequence and quietly yields an empty tuple.

  3. After first, *mid, last = (10, 20, 30, 40, 50), what is type(mid)?

    pch.quizShowAnswer

    B — list — A starred assignment target always collects into a list, whatever the source type. mid is [20, 30, 40]. Use tuple(mid) if you need a tuple.

  4. nest = (1, [2, 3], 4). Which statement succeeds?

    pch.quizShowAnswer

    B — nest[1].append(99) — The tuple holds a reference to the list and cannot stop the list from changing itself, so append works. Rebinding nest[0] raises TypeError, and hash(nest) raises because the list inside is unhashable.

In this tutorial, we learned how to access the tuple in Python. We learned how to access the tuple elements using the index number. We learned how to access the tuple elements using the negative index number. We learned how to access the tuple elements using the range of index numbers. We learned how to access the tuple elements using the range of negative index numbers. For more information on tuples, visit the Python Tuple documentation page.


pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading