Open In App

Python | Get first index values in tuple of strings

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Yet another peculiar problem which 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 articles solves problem of extracting only the first 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 to naive approach and this problem isn’t an exception. In this, we just iterate through each list picking just the 0th index element to build the resultant list. 

Python3




# Python3 code to demonstrate
# Get first index values in tuple of strings
# using list comprehension
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using list comprehension
# Get first index values in tuple of strings
res = list(sub[0] for sub in test_tuple)
 
# print result
print("The first index string character list : " + str(res))


Output : 

The original tuple : ('GfG', 'for', 'Geeks')
The first index string character list : ['G', 'f', 'G']

Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(n), as we are creating a new list to store the first character of each string in the tuple.

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. 

Python3




# Python3 code to demonstrate
# Get first index values in tuple of strings
# using next() + zip()
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using next() + zip()
# Get first index values in tuple of strings
res = list(next(zip(*test_tuple)))
 
# print result
print("The first index string character list : " + str(res))


Output : 

The original tuple : ('GfG', 'for', 'Geeks')
The first index string character list : ['G', 'f', 'G']

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 the map() function and a lambda function:

Using the map() function and a lambda function to extract the first index of each string in test_tuple

Python3




# Python3 code to demonstrate
# Get first index values in tuple of strings
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
# printing original tuple
print("The original tuple : " + str(test_tuple))
# use the map() function and a lambda function to extract the first index of each string in test_tuple
res= list(map(lambda x: x[0], test_tuple))
 
# print result
print("The first index string character list : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original tuple : ('GfG', 'for', 'Geeks')
The first index string character list : ['G', 'f', 'G']

Time complexity: O(n)
Auxiliary Space: O(n) for storing result

Method#4: Using for loop

Python3




# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using for loop to extract the first index of each string in test_tuple
res = []
for string in test_tuple:
    res.append(string[0])
 
# print result
print("The first index string character list : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original tuple : ('GfG', 'for', 'Geeks')
The first index string character list : ['G', 'f', 'G']

Time complexity: O(n)
Auxiliary Space: O(n)

Method #5: using re module

Step-by-step algorithm for implementing

  1. Import the required module, re.
  2. Initialize a tuple.
  3. Print the original tuple.
  4. Initialize an empty list, result, which will store the first index string character list.
  5. Loop through each string in the test_tuple.
  6. Using the match() method from the re module, find the first word character of the string.
  7. Store the first index in the first_index variable.
  8. Append the first_index to the result list.
  9. Print the result list.

Python3




import re
 
# initializing tuple
test_tuple = ('GfG', 'for', 'Geeks')
 
# printing original tuple
print("The original tuple : " + str(test_tuple))
 
# using regular expressions to extract the first index of each string in test_tuple
result = []
for string in test_tuple:
    match = re.match(r'(\w)', string)
    first_index = match.group(0)
    result.append(first_index)
 
# print result
print("The first index string character list : " + str(result))


Output

The original tuple : ('GfG', 'for', 'Geeks')
The first index string character list : ['G', 'f', 'G']

Time complexity: O(nk), where n is the length of the tuple and k is the length of the longest string in the tuple. The regular expression match operation is O(k) and it is performed n times.
Auxiliary space: O(n), where n is the length of the tuple. The result list stores n elements. The first_index variable is reused for each string in the tuple.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads