Sunday, November 17, 2024
Google search engine
HomeLanguagesAccess front and rear element of Python tuple

Access front and rear element of Python tuple

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)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments