- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 a elements in a list of Tuples to Float
When it is required to convert elements of a list of tuple into float values, the ‘isalpha’ method can be used to check if an element is an alphabet or not. The ‘float’ method is used to convert the elements of the list of tuple into float values.
Below is a demonstration for the same −
Example
my_list = [("45", "Jane"), ("11", "Will"), ("37.68", "86.78"), ("Rob", "89.90")] print("The list is : ") print(my_list) my_result = [] for tup in my_list: temp_val = [] for elem in tup: if elem.isalpha(): temp_val.append(elem) else: temp_val.append(float(elem)) my_result.append((temp_val[0],temp_val[1])) print("The float values are : " ) print(my_result)
Output
The list is : [('45', 'Jane'), ('11', 'Will'), ('37.68', '86.78'), ('Rob', '89.90')] The float values are : [(45.0, 'Jane'), (11.0, 'Will'), (37.68, 86.78), ('Rob', 89.9)]
Explanation
A list of tuples is defined and is displayed on the console.
An empty list is created.
The elements in the list of tuple is iterated over, and a temporary list is also created.
Every element is called using ‘isalpha’ method.
If it is an alphabet, the element is appended to the temporary list.
Otherwise, it is converted to float value and then appended to the temporary list.
These lists are displayed as output on the console.
- Related Articles
- Python program to convert 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
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Python program to sort a list of tuples alphabetically
- Convert list of tuples into list in Python
- Python program to sort a list of tuples by second Item
- Python - Change the signs of elements of tuples in a list
- Convert list of tuples into digits in Python
- Convert a list to string in Python program

Advertisements