- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to convert elements in a list of Tuples to Float
When it is required to convert elements in a list of tuples to float values, the ‘isalpha’ method, the ‘float’ method, and a simple iteration is used.
Below is a demonstration of the same −
my_list = [("31", "py"), ("22", "226.65"), ("18.12", "17"), ("pyt", "12")] print("The list is :") print(my_list) my_result =  for index in my_list: my_temp =  for element in index: if element.isalpha(): my_temp.append(element) else: my_temp.append(float(element)) my_result.append((my_temp,my_temp)) print("The result is :") print(my_result)
The list is : [('31', 'py'), ('22', '226.65'), ('18.12', '17'), ('pyt', '12')] The result is : [(31.0, 'py'), (22.0, 226.65), (18.12, 17.0), ('pyt', 12.0)]
A list of list with integers is defined and is displayed on the console.
An empty list is declared.
The list is iterated over, and the element is checked for alphabet using isalpha() function.
If the condition is satisfied, the element is appended as it is and if the condition fails, the element is converted to float and appended.
This result is assigned to a variable.
This is the output that is displayed on the console.
- Python program to Convert a elements in a list of Tuples to Float
- Python program to find Tuples with positive elements in a List of tuples
- Python program to convert a list of tuples into Dictionary
- Python program to find Tuples with positive elements in List of tuples
- Convert list of tuples to list of list in Python
- Convert list of strings to list of tuples in Python
- Convert list of tuples to list of strings in Python
- Convert dictionary to list of tuples in Python
- Convert list of tuples into list in Python
- Python program to sort a list of tuples alphabetically
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Convert list of tuples into digits in Python
- How to convert Float array list to float array in Java?
- Python - Change the signs of elements of tuples in a list
- Python program to sort a list of tuples by second Item