Access front and rear element of Python tuple
Last Updated :
12 Apr, 2023
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
test_tup = ( 10 , 4 , 5 , 6 , 7 )
print ( "The original tuple : " + str (test_tup))
res = (test_tup[ 0 ], test_tup[ - 1 ])
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
from operator import itemgetter
test_tup = ( 10 , 4 , 5 , 6 , 7 )
print ( "The original tuple : " + str (test_tup))
res = itemgetter( 0 , - 1 )(test_tup)
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
test_tup = ( 10 , 4 , 5 , 6 , 7 )
print ( "The original tuple : " + str (test_tup))
res = (test_tup[ 0 ], test_tup[ len (test_tup) - 1 ])
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
test_tup = ( 10 , 4 , 5 , 6 , 7 )
print ( "The original tuple : " + str (test_tup))
front, * _, rear = test_tup
res = (front, rear)
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(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:
- Initialize the tuple “test_tup” with values (10, 4, 5, 6, 7).
- Print the original tuple “test_tup”.
- Use indexing/slicing to access the first element of the tuple and store it in a variable “front”.
- Use negative indexing/slicing to access the last element of the tuple and store it in a variable “rear”.
- Create a new tuple “res” with the front and rear elements stored in the previous steps.
- Print the new tuple “res” containing the front and rear elements of the original tuple.
Python3
test_tup = ( 10 , 4 , 5 , 6 , 7 )
print ( "The original tuple : " + str (test_tup))
front = test_tup[ 0 ]
rear = test_tup[ - 1 ]
res = (front, rear)
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
test_tup = ( 10 , 4 , 5 , 6 , 7 )
print ( "The original tuple : " + str (test_tup))
res = tuple ([test_tup[i] for i in [ 0 , - 1 ]])
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)
Please Login to comment...