![]() |
VOOZH | about |
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 Nth index element of each string in 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 Nth index element to build the resultant list.
The original tuple : ('GfG', 'for', 'Geeks')
The Nth index string character list : ['f', 'o', 'e']Time complexity: O(n), where n is the length of the tuple. This is because the list comprehension iterates through each element of the tuple and extracts the Nth index string character from each element.
Auxiliary space: O(n), where n is the length of the tuple. This is because the program creates a list with n elements, where each element is a string character extracted from the corresponding tuple element.
Method #2: Using next() + zip() This particular task can also be performed using the combination of above two in more efficient way, using the iterators to do this task. The zip function can be used bind together the string elements.
steps to implement this approach:
The original tuple : ('GfG', 'for', 'Geeks')
The Nth index string character list : ['f', 'o', 'e']Time Complexity: O(n*n), where n is the length of the input list. This is because we’re using next() + zip() which has a time complexity of O(n*n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method 3: Using map() and lambda
We can use the map() function along with a lambda function to extract the Nth index character from each string in the tuple. The map() function applies the lambda function to each element of the tuple and returns a map object. We then convert this map object to a list using the list() function and store the result in the variable res. Finally, we print the result.
The original tuple : ('GfG', 'for', 'Geeks')
The Nth index string character list : ['f', 'o', 'e']Time complexity: O(N), where N is the number of elements in the given tuple. This is because the program loops through each element of the tuple once to extract the Nth index character.
Auxiliary space: O(N), as the program creates a new list res of size N to store the Nth index character from each string in the tuple.
Method 4: Use a for loop to iterate through the tuples, and then use string slicing to extract the Nth character from each string in the tuple.
Step-by-step approach:
Below is the implementation of the above approach:
The original tuple : ('GfG', 'for', 'Geeks')
The Nth index string character list : ['f', 'o', 'e']Time complexity: O(n), where n is the number of strings in the tuple
Auxiliary space: O(n), where n is the number of strings in the tuple, since we are storing the Nth character of each string in a list.
Method 5: Using the str.join() method and list comprehension.
Step-by-step approach:
The original tuple : ('GfG', 'for', 'Geeks')
The Nth index string character list : ['f', 'o', 'e']Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(n), where n is the length of the tuple (to store the list of extracted characters).
Method 6: Using list() and enumerate() function
Step-by-step approach:
Below is the implementation of the above approach:
The original tuple : ('GfG', 'for', 'Geeks')
The Nth index string character list : ['f', 'o', 'e']Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(n), where n is the length of the tuple.