Open In App

Python – Get Nth column elements in Tuple Strings

Last Updated : 09 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 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. 

Python3




# Python3 code to demonstrate
# Nth column in Tuple Strings
# using list comprehension
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# initializing N
N = 1
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using list comprehension
# Nth column in Tuple Strings
res = list(sub[N] for sub in test_tuple)
 
# print result
print("The Nth index string character list : " + str(res))


Output : 

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:

  1. Initialize the tuple.
  2. Print the original tuple.
  3. Initialize the index of the column to be extracted.
  4. Use the zip() function with the asterisk operator to convert the tuple into a list of individual characters.
  5. Use a for loop to iterate through the list up to the index of the desired column.
  6. Use the next() function to move the iterator to the desired column.
  7. Convert the column iterator to a list to extract the characters in the desired column.
  8. Print the list of characters in the desired column.

Python3




# Python3 code to demonstrate
# Nth column in Tuple Strings
# using next() + zip()
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing N
N = 1
 
# using next() + zip()
# Nth column in Tuple Strings
temp = zip(*test_tuple)
for idx in range(0, N):
    next(temp)
res = list(next(temp))
 
# print result
print("The Nth index string character list : " + str(res))


Output : 

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.

Python3




# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# initializing N
N = 1
 
# printing original tuple
print("The original tuple : ", test_tuple)
 
# using map() and lambda
# Nth column in Tuple Strings
res = list(map(lambda sub: sub[N], test_tuple))
 
# print result
print("The Nth index string character list : ", res)


Output

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:

  • Initialize a tuple named test_tuple with three string values: ‘GfG’, ‘for’, and ‘Geeks’.
  • Print the original tuple using the print() function and string concatenation.
  • Initialize the value of N to 1. This will be used to extract the second column of each string in the tuple.
  • Create an empty list named res, which will be used to store the characters from the Nth column of each string.
  • Using a for loop, iterate through each string in the test_tuple.
    • Inside the for loop, use string slicing to extract the character at index N (which is the second column in this case). Append this character to the res list.
  • Print the list res which contains the characters from the Nth column of each string in the tuple.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# Nth column in Tuple Strings
# using a for loop and string slicing
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing N
N = 1
 
# using a for loop and string slicing
# Nth column in Tuple Strings
res = []
for string in test_tuple:
    res.append(string[N])
 
# print result
print("The Nth index string character list : " + str(res))


Output

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:

  1. Initialize the tuple with the given strings.
  2. Print the original tuple.
  3. Initialize the value of N to the index of the character to be extracted.
  4. Use list comprehension to iterate through the strings in the tuple and extract the Nth character from each string.
  5. Store the extracted characters in a list.
  6. Print the resulting list.

Python3




# Python3 code to demonstrate
# Nth column in Tuple Strings
# using join() and list comprehension
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing N
N = 1
 
# using join() and list comprehension
# Nth column in Tuple Strings
res = [string[N] for string in test_tuple]
 
# print result
print("The Nth index string character list : " + str(res))


Output

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:

  • Initialize the tuple, test_tuple.
  • Initialize the value of N, which is the index of the character to be extracted from each string.
  • Use the list() and enumerate() functions to create a list of tuples, where each tuple contains the index of the string and the string itself.
  • Use a list comprehension to extract the Nth character from each string in the list of tuples.
  • Return the list of extracted characters.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# Nth column in Tuple Strings
# using list() and enumerate()
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# initializing N
N = 1
 
# using list() and enumerate()
# Nth column in Tuple Strings
res = [t[N] for i, t in enumerate(list(test_tuple))]
 
# print result
print("The Nth index string character list : " + str(res))


Output

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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads