Open In App

Python | Rear elements from Tuple Strings

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

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 tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using list comprehsion
# Rear elements from Tuple Strings
res = list(sub[len(sub) - 1] for sub in test_tuple)
 
# print result
print("The rear index string character list : " + str(res))


Output

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 tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using list comprehsion
# Rear elements from Tuple Strings
res = []
for sub in test_tuple:
    N = len(sub) - 1
    res.append(sub[N])
 
# print result
print("The rear index string character list : " + str(res))


Output

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 tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
 
# Rear elements from Tuple Strings
res=[]
for i in test_tuple:
    res.append(i[-1])
# print result
print("The rear index string character list : " + str(res))


Output

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 tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
 
# Rear elements from Tuple Strings
res = list(map(lambda x: x[-1], test_tuple))
 
# print result
print("The rear index string character list : " + str(res))


Output

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 tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using list() and reversed() functions
# Rear elements from Tuple Strings
lst = list(test_tuple)
lst.reverse()
res = [s[-1] for s in lst]
res.reverse()
 
# print result
print("The rear index string character list : " + str(res))


Output

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

  1. Initialize the tuple
  2. Use slicing to get the last character of each string in the tuple
  3. Reverse the list of characters
  4. Print the result

Python3




# Python3 code to demonstrate
# Rear elements from Tuple Strings
# using slicing
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using slicing to get the last character of each string
res = [s[-1] for s in test_tuple[::-1]]
 
# reverse the list of characters
res.reverse()
 
# print result
print("The rear index string character list : " + str(res))


Output

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads