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.
Similar Reads
Get Index of Values in Python Dictionary
Dictionary values are lists and we might need to determine the position (or index) of each element within those lists. Since dictionaries themselves are unordered (prior to Python 3.7) or ordered based on insertion order (in Python 3.7+), the concept of "index" applies to the valuesâspecifically whe
3 min read
How to Get Index of a Substring in Python?
To get index of a substring within a Python string can be done using several methods such as str.find(), str.index(), and even regular expressions. Each approach has its own use case depending on the requirements. Letâs explore how to efficiently get the index of a substring. The simplest way to get
2 min read
Python | Merge Tuple String List values to String
Sometimes, while working with records, we can have a problem in which any element of record can be of type string but mistakenly processed as list of characters. This can be a problem while working with a lot of data. Let's discuss certain ways in which this problem can be solved. Method #1: Using l
6 min read
Find the Index of a Substring in Python
Finding the position of a substring within a string is a common task in Python. In this article, we will explore some simple and commonly used methods to find the index of a substring in Python. Using str.find() The find() method searches for the first occurrence of the specified substring and retur
3 min read
Python - Get the indices of Uppercase characters in given string
Given a String extract indices of uppercase characters. Input : test_str = 'GeeKsFoRGeeks' Output : [0, 3, 5, 7, 8] Explanation : Returns indices of uppercase characters. Input : test_str = 'GFG' Output : [0, 1, 2] Explanation : All are uppercase. Method #1 : Using list comprehension + range() + isu
5 min read
How to Find Index of a Substring in Python
In Python, sometimes we need to know where a certain substring appears in a string. To do this, we can find the index (or position) of that substring. The index tells us where the substring is located. Letâs look at some simple ways to find the index of a substring in a string. Using find() methodTh
2 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 - Element Index in Range Tuples
Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
6 min read
Python | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Python | Group tuples in list with same first value
Given a list of tuples, the task is to print another list containing tuple of same first element. Below are some ways to achieve above tasks. Example: Input : [('x', 'y'), ('x', 'z'), ('w', 't')] Output: [('w', 't'), ('x', 'y', 'z')] Method #1: Using extend C/C++ Code # Python code to find common #
7 min read