How toGet First and Last Elements from a Tuple in Python Last Updated : 07 Jun, 2024 Comments Improve Suggest changes Like Article Like Report 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. Get First and Last Elements from a Tuple Using IndexingWe can use indexing to access the first and last elements of the tuple. Python indexing starts at 0 for the first element and -1 for last element. Python # Create a tuple my_tuple = (10, 20, 30, 40, 50) # Get the first element first_elements = my_tuple[0] # Get the last element last_elements = my_tuple[-1] print("First Element:", first_elements) print("Last Element:", last_elements) Output : First Element: 10 Last Element: 50Get First and Last Elements from a Tuple Using Tuple UnpackingThe 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: Python # 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: 50Get First and Last Elements from a Tuple Using SlicingWe can also use slicing to get the first and last elements of a tuple. Slicing allows you to the specify a range of indices to the extract elements: Python # Create a tuple my_tuple = (10, 20, 30, 40, 50) # Get the first element using the slicing first_elements = my_tuple[:1][0] # Get the last element using the slicing last_elements = my_tuple[-1:] print("First Element:", first_elements) print("Last Element:", last_elements[0]) Output : First Element: 10 Last Element: 50 Comment More infoAdvertise with us Next Article How toGet First and Last Elements from a Tuple in Python M maha123 Follow Improve Article Tags : Python python-tuple Practice Tags : python Similar Reads How to get the first and last elements of Deque in Python? Deque is a Double Ended Queue which is implemented using the collections module in Python. Let us see how can we get the first and the last value in a Deque. Method 1: Accessing the elements by their index. The deque data structure from the collections module does not have a peek method, but similar 2 min read Python - Find first element by second in tuple List Sometimes, while working with Python records, we can have a problem in which we need to find the first element of tuple from the given second element. This kind of problem can occur in domains such as web development. Let's discuss certain ways in which this task can be performed. Input : test_list 4 min read Python - Accessing nth element from tuples in list We are given tuples in list we need to access Nth elements from tuples. For example, we are having a list l = [(1, 2), (3, 4), (5, 6)] we need to access the Nth element from tuple suppose we need to access n=1 or 1st element from every tuple present inside list so that in this case the output should 2 min read How to Take a Tuple as an Input in Python? Tuples are immutable data structures in Python making them ideal for storing fixed collections of items. In many situations, you may need to take a tuple as input from the user. Let's explore different methods to input tuples in Python.The simplest way to take a tuple as input is by using the split( 3 min read How to Get First N Items from a List in Python Accessing elements in a list has many types and variations. For example, consider a list "l = [1,2,3,4,5,6,7]" and N=3, for the given N, our required result is = [1,2,3].This article discusses several ways to fetch the first N elements of a given list.Method 1: Using List SlicingThis problem can be 3 min read Like