Given a Tuple list, convert all possible convertible elements to float.
Input : test_list = [(“3”, “Gfg”), (“1”, “26.45”)]
Output : [(3.0, ‘Gfg’), (1.0, 26.45)]
Explanation : Numerical strings converted to floats.Input : test_list = [(“3”, “Gfg”)]
Output : [(3.0, ‘Gfg’)]
Explanation : Numerical strings converted to floats.
Method #1 : Using loop + isalpha() + float()
In this, we use a loop to iterate for all the tuples, check for alphabets using isalpha(), which cannot be converted to float, and for the rest of the elements float() is used to convert.
Python3
# Python3 code to demonstrate working of # Convert Tuple List elements to Float # Using loop + isalpha() + float # initializing list test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )] # printing original list print ( "The original list is : " + str (test_list)) res = [] for tup in test_list: temp = [] for ele in tup: # check for string if ele.isalpha(): temp.append(ele) else : # convert to float temp.append( float (ele)) res.append((temp[ 0 ],temp[ 1 ])) # printing result print ( "The converted list : " + str (res)) |
Output:
The original list is : [(‘3’, ‘Gfg’), (‘1’, ‘26.45’), (‘7.32’, ‘8’), (‘Gfg’, ‘8’)] The converted list : [(3.0, ‘Gfg’), (1.0, 26.45), (7.32, 8.0), (‘Gfg’, 8.0)]
Time complexity: O(nm), where n is the length of the input list and m is the maximum number of elements in each tuple.
Auxiliary space: O(nm).
Method #2 : Using loop + isalpha() + float() + list comprehension
In this, we perform the task of iterating through inner tuples using list comprehension.
Python3
# Python3 code to demonstrate working of # Convert Tuple List elements to Float # Using loop + isalpha() + float # initializing list test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )] # printing original list print ( "The original list is : " + str (test_list)) res = [] for tup in test_list: # list comprehension to check for each case temp = [ele if ele.isalpha() else float (ele) for ele in tup] res.append((temp[ 0 ],temp[ 1 ])) # printing result print ( "The converted list : " + str (res)) |
Output:
The original list is : [(‘3’, ‘Gfg’), (‘1’, ‘26.45’), (‘7.32’, ‘8’), (‘Gfg’, ‘8’)] The converted list : [(3.0, ‘Gfg’), (1.0, 26.45), (7.32, 8.0), (‘Gfg’, 8.0)]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 3: Using map() function and lambda expression
Use the map() function along with a lambda expression to achieve the same result.
Python3
# Python3 code to demonstrate working of # Convert Tuple List elements to Float # Using map() + lambda # initializing list test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )] # printing original list print ( "The original list is : " + str (test_list)) # using map() + lambda to convert tuple elements to float res = list ( map ( lambda tup: ( float (tup[ 0 ]) if not tup[ 0 ].isalpha() else tup[ 0 ], float (tup[ 1 ]) if not tup[ 1 ].isalpha() else tup[ 1 ]), test_list)) # printing result print ( "The converted list : " + str (res)) |
The original list is : [('3', 'Gfg'), ('1', '26.45'), ('7.32', '8'), ('Gfg', '8')] The converted list : [(3.0, 'Gfg'), (1.0, 26.45), (7.32, 8.0), ('Gfg', 8.0)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Method 4: Using list comprehension + try-except
You can use list comprehension and try-except to handle conversion of the tuple elements to float. This approach is similar to Method but uses list comprehension instead of the map() function.
Python3
# initializing list test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )] # printing original list print ( "The original list is : " + str (test_list)) # using list comprehension + try-except to convert tuple elements to float res = [( float (t[ 0 ]) if isinstance (t[ 0 ], str ) and t[ 0 ].replace( '.' , '').isdigit() else t[ 0 ], float (t[ 1 ]) if isinstance (t[ 1 ], str ) and t[ 1 ].replace( '.' , '').isdigit() else t[ 1 ]) for t in test_list] # printing result print ( "The converted list : " + str (res)) |
The original list is : [('3', 'Gfg'), ('1', '26.45'), ('7.32', '8'), ('Gfg', '8')] The converted list : [(3.0, 'Gfg'), (1.0, 26.45), (7.32, 8.0), ('Gfg', 8.0)]
The time complexity is O(n), where n is the length of the input list, since it involves iterating over each element of the list and performing constant time operations.
The auxiliary space complexity is also O(n), since it creates a new list with the same length as the input list to store the converted tuples.
Method 5 : Using try-except block
In this method, we will use a try-except block to convert the numeric elements of the tuple to float. If a non-numeric element is encountered, it will be kept as it is.
Algorithm:
Create an empty list to store the converted tuples.
Iterate over each tuple in the given list.
Use a try-except block to convert the first element of the tuple to float.
If the conversion is successful, then append the converted tuple to the result list.
If the conversion fails, then append the original tuple to the result list.
Return the result list.
Print the original and converted lists.
Python3
# Python3 code to demonstrate working of # Convert Tuple List elements to Float # Using try-except block # initializing list test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )] # printing original list print ( "The original list is : " + str (test_list)) # creating empty list for result res = [] # iterating over each tuple in the given list for tup in test_list: try : # converting the first element of the tuple to float temp = ( float (tup[ 0 ]), tup[ 1 ]) res.append(temp) except ValueError: # keeping the original tuple if conversion fails res.append(tup) # printing result print ( "The converted list : " + str (res)) |
The original list is : [('3', 'Gfg'), ('1', '26.45'), ('7.32', '8'), ('Gfg', '8')] The converted list : [(3.0, 'Gfg'), (1.0, '26.45'), (7.32, '8'), ('Gfg', '8')]
Time complexity: O(n), where n is the length of the given list of tuples.
Auxiliary space: O(n), where n is the length of the given list of tuples.