Python | Get first index values in tuple of strings
Last Updated :
24 Mar, 2023
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
test_tuple = ( 'GfG' , 'for' , 'Geeks' )
print ("The original tuple : " + str (test_tuple))
res = list (sub[ 0 ] for sub in test_tuple)
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
test_tuple = ( 'GfG' , 'for' , 'Geeks' )
print ("The original tuple : " + str (test_tuple))
res = list ( next ( zip ( * test_tuple)))
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
test_tuple = ( 'GfG' , 'for' , 'Geeks' )
print ( "The original tuple : " + str (test_tuple))
res = list ( map ( lambda x: x[ 0 ], test_tuple))
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)
Auxiliary Space: O(n) for storing result
Method#4: Using for loop
Python3
test_tuple = ( 'GfG' , 'for' , 'Geeks' )
print ( "The original tuple : " + str (test_tuple))
res = []
for string in test_tuple:
res.append(string[ 0 ])
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)
Auxiliary Space: O(n)
Method #5: using re module
Step-by-step algorithm for implementing
- Import the required module, re.
- Initialize a tuple.
- Print the original tuple.
- Initialize an empty list, result, which will store the first index string character list.
- Loop through each string in the test_tuple.
- Using the match() method from the re module, find the first word character of the string.
- Store the first index in the first_index variable.
- Append the first_index to the result list.
- Print the result list.
Python3
import re
test_tuple = ( 'GfG' , 'for' , 'Geeks' )
print ( "The original tuple : " + str (test_tuple))
result = []
for string in test_tuple:
match = re.match(r '(\w)' , string)
first_index = match.group( 0 )
result.append(first_index)
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
Please Login to comment...