Open In App

Access front and rear element of Python tuple

Last Updated : 12 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with records, we can have a problem in which we need to access the initial and last data of a particular record. This kind of problem can have application in many domains. Let’s discuss some ways in which this problem can be solved. 

Method #1: Using Access Brackets We can perform the possible get of front and rear element in tuple using the access brackets in similar way in which elements can be accessed in list. 

Python3




# Python3 code to demonstrate working of
# Accessing front and rear element of tuple
# using access brackets
 
# initialize tuple
test_tup = (10, 4, 5, 6, 7)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Accessing front and rear element of tuple
# using access brackets
res = (test_tup[0], test_tup[-1])
 
# printing result
print("The front and rear element of tuple are : " + str(res))


Output : 

The original tuple : (10, 4, 5, 6, 7)
The front and rear element of tuple are : (10, 7)

Time complexity: O(1)
Auxiliary space: O(1)

Method #2: Using itemegetter() This is yet another way in which this task can be performed. In this, we access the elements using inbuilt function of itemgetter(). 

Python3




# Python3 code to demonstrate working of
# Accessing front and rear element of tuple
# using itemgetter()
from operator import itemgetter
 
# initialize tuple
test_tup = (10, 4, 5, 6, 7)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Accessing front and rear element of tuple
# using itemgetter()
res = itemgetter(0, -1)(test_tup)
 
# printing result
print("The front and rear element of tuple are : " + str(res))


Output : 

The original tuple : (10, 4, 5, 6, 7)
The front and rear element of tuple are : (10, 7)

Time complexity: O(1), as it only performs constant time operations (i.e., accessing elements of a tuple).
Auxiliary space: O(1), as it does not use any additional data structures that grow with the size of the input.

Method #3: Using indexing

Python3




# Python3 code to demonstrate working of
# Accessing front and rear element of tuple
# using access brackets
 
# initialize tuple
test_tup = (10, 4, 5, 6, 7)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Accessing front and rear element of tuple
# using access brackets
res = (test_tup[0], test_tup[len(test_tup)-1])
 
# printing result
print("The front and rear element of tuple are : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 7)
The front and rear element of tuple are : (10, 7)

Time Complexity : O(1)
Auxiliary Space : O(1)

Method#4: Using Unpacking tuple.

This method requires the *_ syntax, which is called “unpacking” in Python and allows you to assign a specific portion of a tuple to a variable. In this case, the *_ syntax is used to ignore the intermediate elements in the tuple.

Python3




# Python3 code to demonstrate working of
# Accessing front and rear element of tuple
# using Unpacking the tuple
 
# initialize tuple
test_tup = (10, 4, 5, 6, 7)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Accessing front and rear element of tuple
# Unpacking the tuple
front, *_, rear = test_tup
 
# Storing the front and rear elements
res = (front, rear)
 
 
# printing result
print("The front and rear element of tuple are : " + str(res))
 
# this code contributed by tvsk.


Output

The original tuple : (10, 4, 5, 6, 7)
The front and rear element of tuple are : (10, 7)

Time Complexity: O(1)
Auxiliary Space: O(n), where n is the length of the tuple. This is because the *_ syntax creates a new list with length n-2, which takes up memory space. 

Method #5: Using slicing

This program demonstrates how to access the first and last elements of a tuple using slicing in Python. It initializes a tuple, extracts its first and last elements using slicing, stores them in a new tuple, and prints the result.

Approach:

  1. Initialize the tuple “test_tup” with values (10, 4, 5, 6, 7).
  2. Print the original tuple “test_tup”.
  3. Use indexing/slicing to access the first element of the tuple and store it in a variable “front”.
  4. Use negative indexing/slicing to access the last element of the tuple and store it in a variable “rear”.
  5. Create a new tuple “res” with the front and rear elements stored in the previous steps.
  6. Print the new tuple “res” containing the front and rear elements of the original tuple.

Python3




# Python3 code to demonstrate working of
# Accessing front and rear element of tuple
# using slicing
 
# initialize tuple
test_tup = (10, 4, 5, 6, 7)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Accessing front and rear element of tuple
# using slicing
front = test_tup[0]
rear = test_tup[-1]
 
# Storing the front and rear elements
res = (front, rear)
 
# printing result
print("The front and rear element of tuple are : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 7)
The front and rear element of tuple are : (10, 7)

Time complexity: O(1) as it only accesses the first and last elements of the tuple using indexing/slicing. 
Auxiliary space: O(1) as it only creates two variables to store the first and last elements of the tuple.

Method #6: Using tuple() constructor and concatenation

In this method, first retrieve the first and last elements of the original tuple using indexing. Then, create two new tuples containing these elements using the tuple() constructor. Finally, concatenate these two tuples using the + operator to create a new tuple containing both elements.

Python3




test_tup = (10, 4, 5, 6, 7)
 
front = test_tup[0]
rear = test_tup[-1]
 
res = tuple([front]) + tuple([rear])
 
print("The front and rear element of tuple are : " + str(res))


Output

The front and rear element of tuple are : (10, 7)

Time complexity: O(1) because it only accesses specific elements in the tuple and performs constant-time operations to create the new tuple.
Auxiliary space: O(1) because it only uses a constant amount of extra memory to store the front and rear elements, as well as the new tuple.

Method #7: Using List Comprehension

  • Create a list comprehension with the first and last element of the tuple.
  • Convert the resulting list to a tuple using the tuple() constructor.

Python3




# initialize tuple
test_tup = (10, 4, 5, 6, 7)
 
# printing original tuple
print("The original tuple : " + str(test_tup))
 
# Using List Comprehension
res = tuple([test_tup[i] for i in [0,-1]])
 
# printing result
print("The front and rear element of tuple are : " + str(res))


Output

The original tuple : (10, 4, 5, 6, 7)
The front and rear element of tuple are : (10, 7)

Time complexity: O(1)
Auxiliary space: O(1)



Previous Article
Next Article

Similar Reads

Python | Front and rear range deletion in a list
Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element deletion. It is a good utility whose solution can be useful to have. Let's discuss certain ways in which this can be performed. Method #1: Using list slicing + del operator The del
7 min read
Python | Append at front and remove from rear
Being familiar with the concept of queue, which follows the FIFO rule, i.e first in first out, that suggests a front removal and rear insertion. This has been discussed many times. But sometimes we need to perform the exact opposite of this and we need to perform the append at the front and remove the element from the rear. Let's discuss certain wa
8 min read
Python | Strings with similar front and rear character
Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method by which this ta
4 min read
Python | Retain K Front and Rear elements
Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element retention and deletion of rest elements. It is a good utility whose solution can be useful to have. Let’s discuss certain ways in which this can be performed. Method #1: Using list
8 min read
Python - Strip front and rear Punctuations from given String
Given a string strip rear and front punctuations. Input : test_str = '%$Gfg is b!!est(*^' Output : Gfg is b!!est Explanation : Front and rear punctuations are stripped. Input : test_str = '%Gfg is b!!est(*^' Output : Gfg is b!!est Explanation : Front and rear punctuations are stripped. Method #1 : Using punctuation() + loop In this, we use punctuat
6 min read
Python | Alternate front - rear Sum
While working with python, we usually come by many problems that we need to solve in day-day and in development. Specially in development, small tasks of python are desired to be performed in just one line. We discuss some ways to compute a list consisting of elements that are alternate front-rear sum in the list. Method #1: Using loop This is brut
7 min read
Python - Dictionary construction from front-rear key values
Given a list, construct a dictionary using the key as first half values and values starting from last. Input : test_list = [4, 10, 5, 3] Output : {4: 3, 10: 5} Explanation : First (4) is paired with rear (3) and so on. Input : test_list = [5, 3] Output : {5: 3} Explanation : First (5) is paired with rear (3).Method #1: Using loop This is brute way
6 min read
Python | Shift from Front to Rear in List
The cyclic rotations have been discussed in the earlier articles. But sometimes, we just require a specific task, a part of rotation i.e shift first element to last element in list. This has the application in day-day programming in certain utilities. Let’s discuss certain ways in which this can be achieved. Method #1: Using list slicing and “+” op
7 min read
Python | Rear elements from Tuple Strings
Yet another peculiar problem that might not be common, but can occur in python programming while playing with tuples. Since tuples are immutable, they are difficult to manipulate and hence knowledge of possible variation solutions always helps. This article solves the problem of extracting only the rear index element of each string in a tuple. Let’
5 min read
Python - Concatenate Rear elements in Tuple List
Given a tuple list where we need to concatenate rear elements. Input : test_list = [(1, 2, "Gfg"), ("Best", )] Output : Gfg Best Explanation : Last elements are joined. Input : test_list = [(1, 2, "Gfg")] Output : Gfg Explanation : Last elements are joined. Method #1 : Using list comprehension + join() In this, we check for last element using "-1",
6 min read
Practice Tags :
three90RightbarBannerImg