Python | Rear elements from Tuple Strings
Last Updated :
17 Apr, 2023
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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 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))
OutputThe 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.
Similar Reads
Python | Removing strings from tuple
Sometimes we can come across the issue in which we receive data in form of tuple and we just want the numbers from it and wish to erase all the strings from them. This has a useful utility in Web-Development and Machine Learning as well. Let's discuss certain ways in which this particular task can b
4 min read
Python | Removing duplicates from tuple
Many times, while working with Python tuples, we can have a problem removing duplicates. This is a very common problem and can occur in any form of programming setup, be it regular programming or web development. Let's discuss certain ways in which this task can be performed. Method #1 : Using set()
4 min read
Python - Get Nth column elements in 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
8 min read
Python | Remove all strings from a list of tuples
Given a list of tuples, containing both integer and strings, the task is to remove all strings from list of tuples. Examples: Input : [(1, 'Paras'), (2, 'Jain'), (3, 'GFG'), (4, 'Cyware')] Output : [(1), (2), (3), (4)] Input : [('string', 'Geeks'), (2, 225), (3, '111')] Output : [(), (2, 225), (3,)]
8 min read
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 pe
6 min read
Python | List of tuples to String
Many times we can have a problem in which we need to perform interconversion between strings and in those cases, we can have a problem in which we need to convert a tuple list to raw, comma separated string. Let's discuss certain ways in which this task can be performed. Method #1: Using str() + str
8 min read
Python - Rear element extraction from list of tuples records
While working with tuples, we store different data as different tuple elements. Sometimes, there is a need to print specific information from the tuple like rear index. For instance, a piece of code would want just names to be printed on all the student data. Let's discuss certain ways in which one
8 min read
Python - Tuple elements inversions
Sometimes, while programming, we have a problem in which we might need to perform certain bitwise operations among tuple elements. This is an essential utility as we come across bitwise operations many times. Letâs discuss certain ways in which this task can be performed. Method #1 : Using map() + l
3 min read
Swap elements in String list - Python
Swapping elements in a string list means we need to exchange one element with another throughout the entire string list in Python. This can be done using various methods, such as using replace(), string functions, regular expressions (Regex), etc. For example, consider the original list: ['Gfg', 'is
3 min read
Python - Convert String Records to Tuples Lists
Sometimes, while working with data, we can have problem in which we need to convert the data list which in string format to list of tuples. This can occur in domains in which we have cross type inputs. Lets discuss certain ways in which this task can be performed. Method #1 : Using loop + eval() The
7 min read