Tuples in Python are immutable data structures that we use to store data sequentially. We can access elements from a tuple in O(1) time. This article discusses how to access elements from a tuple in Python.
Access the First Element From a Tuple in Python
We can use the Python indexing operator to access elements from a tuple in Python. As Python follows 0-based indexing, we can use the indexing operator to read the first element of a tuple as shown below.
myTuple=(11,2,44,21,15,4,23,12)
print("The tuple is:")
print(myTuple)
index=0
value=myTuple[index]
print("The first element is:")
print(value)
Output:
The tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The first element is:
11
In 0-based indexing, the index of the first element is 0. Hence, you can observe that we used index 0 to read the first element from the tuple in the above code.
Access The Last Element of a Tuple in Python
To access the last element of a tuple in Python, we will first find the length of the tuple using the len()
function. The len()
function takes the tuple as its input and returns the total number of elements in the tuple. Once we get the length of the list, we can access the last element at the index length of tuple -1
. You can observe this in the following example.
myTuple=(11,2,44,21,15,4,23,12)
print("The tuple is:")
print(myTuple)
length=len(myTuple)
index=length-1
value=myTuple[index]
print("The last element is:")
print(value)
Output:
The tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The last element is:
12
Python also allows negative indices. Negative indexing starts at -1 and is the last element’s position. The second last element is at index -2, the third last at -3, and so on. You can access the last element of a tuple using a negative index as shown below.
myTuple=(11,2,44,21,15,4,23,12)
print("The tuple is:")
print(myTuple)
index=-1
value=myTuple[index]
print("The last element is:")
print(value)
Output:
The tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The last element is:
12
Access the Nth Element From the Start in A Tuple
The Nth element from the start of a tuple will always be present at index N-1. Hence, we can access the Nth element from the start using the indexing operator as shown below.
myTuple=(11,2,44,21,15,4,23,12)
print("The tuple is:")
print(myTuple)
position=4
index=position-1
value=myTuple[index]
print("The 4th element from start is:")
print(value)
Output:
The tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The 4th element from start is:
21
If the number N exceeds the length of the tuple, the program will run into an index error exception. Hence, you should ensure that N must be less than the length of the tuple.
Read Nth Element From the End in a Tuple in Python
The Nth element from the end of a list exists at the negative index -N
. Hence, we can access the Nth element from the end of a list using the indexing operator as shown below.
myTuple=(11,2,44,21,15,4,23,12)
print("The tuple is:")
print(myTuple)
position=4
index= -position
value=myTuple[index]
print("The 4th element from end is:")
print(value)
Output:
The tuple is:
(11, 2, 44, 21, 15, 4, 23, 12)
The 4th element from end is:
15
Conclusion
In this article, we discussed how to access elements at different positions in a tuple in Python. To learn more about Python programming, you can read this article on how to sort a tuple in Python. You might also like this article on set comprehension in Python.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.