Get First and Last Elements from a Tuple Using Tuple Unpacking
The Tuple unpacking allows to the assign the elements of a tuple to the individual variables. By unpacking we can easily get the first and last elements of a tuple:
# Create a tuple
my_tuple = (10, 20, 30, 40, 50)
# Unpack the tuple
first_elements, *_, last_elements = my_tuple
print("First Element:", first_elements)
print("Last Element:", last_elements)
Output :
First Element: 10
Last Element: 50
How toGet First and Last Elements from a Tuple in Python
Tuples are immutable sequences in Python that can store a collection of items. Often, we might need to the retrieve the first and last elements from the tuple. In this article, we’ll explore how to achieve this using the various methods in Python.