Access the Tuple
Navigate Tuple Items: A Guide to Access the Tuple List
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.
Basic of Tuple Indexing
Section titled “Basic of Tuple Indexing”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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0])
print(data[1])
print(data[2])
print(data[3])Output:
C:\Users\username>python tuple_index.py
a
b
c
dIn 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.
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[-1])
print(data[-2])
print(data[-3])
print(data[-4])Output:
C:\Users\username>python tuple_index.py
j
i
h
gIn 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.
Diagram of Tuple Indexing
Section titled “Diagram of Tuple Indexing”The following diagram illustrates the indexing of a tuple with five elements:
my_list = ('a', 'b', 'c', 'd', 'e')| Element | ‘a’ | ‘b’ | ‘c’ | ‘d’ | ‘e’ |
|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 |
| Index (Negative) | -5 | -4 | -3 | -2 | -1 |
Accessing Multidimensional Tuples
Section titled “Accessing Multidimensional Tuples”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:
data = (('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'))
print(data[0])
print(data[1])
print(data[2])Output:
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:
data = (('a', 'b', 'c'), ('d', 'e', 'f'), ('g', 'h', 'i'))
print(data[0][0])
print(data[1][1])
print(data[2][2])Output:
C:\Users\username>python multidimensional_tuple.py
a
e
iIn 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.
Diagram of Multidimensional List Indexing
Section titled “Diagram of Multidimensional List Indexing”| Index | 0 | 1 | 2 |
|---|---|---|---|
| 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.
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:
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.
Accessing tuple Slices
Section titled “Accessing tuple Slices”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:
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.
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:
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.
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:
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.
Access the Tuple Elements Using Step
Section titled “Access the Tuple Elements Using Step”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.
tuple[start:stop:step]Example:
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:
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.
Omitting the Start and Stop Indices
Section titled “Omitting the Start and Stop Indices”In Python, we can omit the start and stop indices. We can omit the start and stop indices by using the following syntax.
tuple[start:]
tuple[:stop]Example:
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[0:])
print(data[:5])
print(data[2:])
print(data[:7])Output:
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.
tuple[start::step]
tuple[:stop:step]Example:
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:
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.
tuple[start::-step]
tuple[:stop:-step]Example:
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:
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]
Omitting the Both Indices
Section titled “Omitting the Both Indices”In Python, we can omit the both indices. We can omit the both indices by using the following syntax.
tuple[::step]Example:
data = ('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j')
print(data[::2])
print(data[::3])
print(data[::4])
print(data[::5])Output:
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]
Slicing with Negative Indices
Section titled “Slicing with Negative Indices”In Python, we can slice with negative indices. We can slice with negative indices by using the following syntax.
tuple[start:stop:step]Example:
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:
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]
Index or slice: two different operations
Section titled “Index or slice: two different operations”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.
flowchart TD
T["t = (10, 20, 30, 40, 50)"] --> Q{"square brackets contain..."}
Q -->|"a single number: t[1]"| I["returns the ELEMENT
20, type int"]
Q -->|"a colon: t[1:3]"| S["returns a NEW TUPLE
(20, 30), type tuple"]
I --> IO{"out of range?"}
S --> SO{"out of range?"}
IO -->|yes| IE["IndexError"]
SO -->|yes| SE["clamped, no error
t[99:] is ()"]
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 rangeSlicing a tuple does not copy it
Section titled “Slicing a tuple does not copy it”t = (10, 20, 30)
print(t[:] is t) # True <- the very same object
nums = [10, 20, 30]
print(nums[:] is nums) # False <- a real copyA 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.
See it move
Section titled “See it move”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.
Unpacking, and the one type surprise
Section titled “Unpacking, and the one type surprise”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).
Immutable container, mutable contents
Section titled “Immutable container, mutable contents”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.
Check yourself
Section titled “Check yourself”-
For a tuple t, what is t[:] is t?
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.
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.
-
t = (10, 20, 30). What do t[99] and t[99:] do?
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.
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.
-
After first, *mid, last = (10, 20, 30, 40, 50), what is type(mid)?
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.
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.
-
nest = (1, [2, 3], 4). Which statement succeeds?
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.
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.
Conclusion
Section titled “Conclusion”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.
Try it: Access Tuple Exercises
Section titled “Try it: Access Tuple Exercises”Exercise 1 – Index Access
Section titled “Exercise 1 – Index Access”Exercise 2 – Tuple Slicing
Section titled “Exercise 2 – Tuple Slicing”Exercise 3 – Nested Tuple Access
Section titled “Exercise 3 – Nested Tuple Access”pch.coffeeTagline
pch.coffeeCtapch.feedbackHeading
pch.feedbackSubheading