Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Get First and Last Elements of Tuple in Python

  • 04 Sep, 2025
  • Com 0
Finding front and last element of tuple in Python

The most efficient and robust way to get the first and last elements of a tuple in Python is to use direct indexing. The tuple[0] returns the first element (0-based index), and the tuple[-1] returns the last element (negative indexing from the end).

Get First and Last Elements of Tuple using indexing in Python

main_tuple = (100, 200, 300, 400, 500)

first_element = main_tuple[0]

print(first_element)

# Output: 100

last_element = main_tuple[-1]

print(last_element)

# Output: 500

Single-element Tuple

Single-element Tuple

If the tuple contains a single element, the first and last elements will be the same because it has only one element.

single_tuple = (5, )

first = single_tuple[0]

print(first)

# Output: 5

last = single_tuple[-1]

print(last)

# Output: 5

Empty tuple

If the tuple is empty and you attempt to find any elements, it will throw IndexError: tuple index out of range

empty_tuple = ()

first_element = empty_tuple[0]

last_element = empty_tuple[-1]

print(first_element)

# Output: IndexError: tuple index out of range

print(last_element)

# Output: IndexError: tuple index out of range

Nested tuples

first and last elements of nested tuple

When it comes to nested tuples, you have two options. Either find the first element of the main tuple or the first element of the nested tuple.

Let’s find the first element and last element of the main tuple, which is itself a tuple due to nesting.

nested = ((1, 2), (3, 4), (5, 6))

first_outer = nested[0]

print(first_outer)

# Output: (1, 2)

last_outer = nested[-1]

print(last_outer)

# Output: (5, 6)

As you can see, the first element itself is a tuple (1, 2), and the last element itself is also a tuple (5, 6).

How about finding the first element of the first element and the last element of the last tuple?

First element of the first tuple and last element of the last tuple

In that case, we need to use nested[0][0] for the first and nested[-1][-1] for the last.

nested = ((1, 2), (3, 4), (5, 6))

first_inner = nested[0][0]

print(first_inner)

# Output: 1

last_outer = nested[-1][-1]

print(last_outer)

# Output: 6

Alternate approaches

Using tuple unpacking (*)

You can use the tuple unpacking with * for clarity when ignoring middle elements.

main_tuple = (101, 201, 301, 401)

first, *middle, last = main_tuple

print(first, last)

# Output: 101 401

Using *middle, we ignored all the middle elements and just printed the first and last elements.

However, it strictly requires Python 3 or later for extended unpacking.

Using slicing

You can use the “slicing” to extract sub-tuples containing the first or last elements.

For the first element, use tuple[:1][0].

For the last element, use tuple[-1:][0].

main_tuple = (101, 201, 301, 401)

first = main_tuple[:1][0]

print(first)

# Output: 101

last = main_tuple[-1:][0]

print(last)

# Output: 401

When you use slicing, it returns a new tuple.

Empty tuple

One of the significant advantages of slicing is that even if the tuple is empty, it handles slices safely without throwing indexing errors. Meaning that if the tuple is empty, the slices will be empty too.

empty_tuple = ()

first_slice = empty_tuple[:1]  # () (empty tuple)

print(first_slice)

# Output: ()

last_slice = empty_tuple[-1:]

print(last_slice)

# Output: ()

As you can see, it returns empty slices without any errors.

Post Views: 5
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Python hex(): Converting an Integer to Hex
How to Convert an Integer to Binary String in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend