
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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 −
Example
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[0],my_temp[1])) print("The result is :") print(my_result)
Output
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)]
Explanation
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.
- Related Articles
- 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 find Tuples with positive elements in List of tuples
- Python program to convert a list of tuples into Dictionary
- 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
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Convert list of tuples into list in Python
- Python program to sort a list of tuples alphabetically
- Convert list of tuples into digits in Python
- How to convert Float array list to float array in Java?
- Python program to convert float decimal to octal number
- Python - Change the signs of elements of tuples in a list

Advertisements