Python program to Convert a elements in a list of Tuples to Float
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
test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )]
print ( "The original list is : " + str (test_list))
res = []
for tup in test_list:
temp = []
for ele in tup:
if ele.isalpha():
temp.append(ele)
else :
temp.append( float (ele))
res.append((temp[ 0 ],temp[ 1 ]))
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
test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )]
print ( "The original list is : " + str (test_list))
res = []
for tup in test_list:
temp = [ele if ele.isalpha() else float (ele) for ele in tup]
res.append((temp[ 0 ],temp[ 1 ]))
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
test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )]
print ( "The original list is : " + str (test_list))
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))
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), 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
test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )]
print ( "The original list is : " + str (test_list))
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]
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)]
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
test_list = [( "3" , "Gfg" ), ( "1" , "26.45" ), ( "7.32" , "8" ), ( "Gfg" , "8" )]
print ( "The original list is : " + str (test_list))
res = []
for tup in test_list:
try :
temp = ( float (tup[ 0 ]), tup[ 1 ])
res.append(temp)
except ValueError:
res.append(tup)
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'), ('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.
Last Updated :
26 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...