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’s discuss certain ways in which this problem can be solved.
Method #1: Using list comprehension
Almost every problem can be solved using list comprehension as a shorthand for a naive approach and this problem isn’t an exception. In this, we just iterate through each list picking just the n -1th index element to build the resultant list.
Python3
# Python3 code to demonstrate# Rear elements from Tuple Strings# using list comprehension# initializing tupletest_tuple = ('GfG', 'for', 'Geeks')# printing original tupleprint("The original tuple : " + str(test_tuple))# using list comprehsion# Rear elements from Tuple Stringsres = list(sub[len(sub) - 1] for sub in test_tuple)# print resultprint("The rear index string character list : " + str(res)) |
The original tuple : ('GfG', 'for', 'Geeks')
The rear index string character list : ['G', 'r', 's']
Method #2: Using loop
This task can also be performed using brute force manner. In this, we just iterate the each string element and extract the rear element when the index reaches size – 1th element.
Python3
# Python3 code to demonstrate# Rear elements from Tuple Strings# using list comprehension# initializing tupletest_tuple = ('GfG', 'for', 'Geeks')# printing original tupleprint("The original tuple : " + str(test_tuple))# using list comprehsion# Rear elements from Tuple Stringsres = []for sub in test_tuple: N = len(sub) - 1 res.append(sub[N])# print resultprint("The rear index string character list : " + str(res)) |
The original tuple : ('GfG', 'for', 'Geeks')
The rear index string character list : ['G', 'r', 's']
The time complexity of the program is O(n), where n is the length of the input tuple test_tuple.
The auxiliary space used by the program is also O(n), as a list res of length n is used to store the rear index string character.
Method #3: Using negative indexing
Python3
# Python3 code to demonstrate# Rear elements from Tuple Strings# initializing tupletest_tuple = ('GfG', 'for', 'Geeks')# printing original tupleprint("The original tuple : " + str(test_tuple))# Rear elements from Tuple Stringsres=[]for i in test_tuple: res.append(i[-1])# print resultprint("The rear index string character list : " + str(res)) |
The original tuple : ('GfG', 'for', 'Geeks')
The rear index string character list : ['G', 'r', 's']
Time complexity: O(n), where n is the length of the test_tuple.
Auxiliary space: O(n), where n is the length of the test_tuple.
Method #4: Using map(),lambda functions
Python3
# Python3 code to demonstrate# Rear elements from Tuple Strings# initializing tupletest_tuple = ('GfG', 'for', 'Geeks')# printing original tupleprint("The original tuple : " + str(test_tuple))# Rear elements from Tuple Stringsres = list(map(lambda x: x[-1], test_tuple))# print resultprint("The rear index string character list : " + str(res)) |
The original tuple : ('GfG', 'for', 'Geeks')
The rear index string character list : ['G', 'r', 's']
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #5: Using list() and reversed() functions
Step-by-step approach:
- Initialize the tuple.
- Use list() function to convert the tuple into a list.
- Use reversed() function to reverse the list.
- Use list comprehension to extract the last character of each string in the list.
- Store the extracted characters in a new list.
- Reverse the new list to get the characters in the original order.
- Print the new list as the result.
Below is the implementation of the above approach:
Python3
# Python3 code to demonstrate# Rear elements from Tuple Strings# using list() and reversed() functions# initializing tupletest_tuple = ('GfG', 'for', 'Geeks')# printing original tupleprint("The original tuple : " + str(test_tuple))# using list() and reversed() functions# Rear elements from Tuple Stringslst = list(test_tuple)lst.reverse()res = [s[-1] for s in lst]res.reverse()# print resultprint("The rear index string character list : " + str(res)) |
The original tuple : ('GfG', 'for', 'Geeks')
The rear index string character list : ['G', 'r', 's']
Time complexity: O(n), where n is the length of the longest string in the tuple.
Auxiliary space: O(n), where n is the total number of characters in the tuple.
Method #6: Using slicing
- Initialize the tuple
- Use slicing to get the last character of each string in the tuple
- Reverse the list of characters
- Print the result
Python3
# Python3 code to demonstrate# Rear elements from Tuple Strings# using slicing# initializing tupletest_tuple = ('GfG', 'for', 'Geeks')# printing original tupleprint("The original tuple : " + str(test_tuple))# using slicing to get the last character of each stringres = [s[-1] for s in test_tuple[::-1]]# reverse the list of charactersres.reverse()# print resultprint("The rear index string character list : " + str(res)) |
The original tuple : ('GfG', 'for', 'Geeks')
The rear index string character list : ['G', 'r', 's']
Time complexity: O(n), where n is the length of the tuple
Auxiliary space: O(n), to store the result list.



