• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Python Tuple Access Element

Python Tuple Access Element

Author: Aditya Raj
Last Updated: April 24, 2023

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.

Table of Contents
  1. Access the First Element From a Tuple in Python
  2. Access The Last Element of a Tuple in Python
  3. Access the Nth Element From the Start in A Tuple
  4. Read Nth Element From the End in a Tuple in Python
  5. Conclusion

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!

Related

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.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us