Python program to convert integer values in a list of tuples to float

Learn: How to to convert integer values in a list of tuples to float in Python? By Shivang Yadav Last updated : December 15, 2023

Problem statement

Here, we have a list of tuples and we need to convert all the integer values in the list of tuples to Float value in Python programming language?

Prerequisites

To understand the solution, you must know the following Python topics:

Converting integer values in a list of tuples to float

From the given list of tuples, we need to convert all the integer values to the float values.

For this we need to check if the value is an integer value i.e. it is not alphabetical (done using isalpha() method) and then for all false values print we will convert them to float values using float() method.

Input:
[("python", 6), ("JavaScript", 9), ("C++", 2)]

Output:
[("python", 6.0), ("JavaScript", 9.0), ("C++", 2.0)]

Here are the methods to convert the integer values to float values,

Approach 1: Using loop, isalpha(), and float()

In this method, we will simply loop over the list and then check for all values that are integer and convert them to float values.

Program

# Python program to convert integer values 
# in a list of tuples to float

# creating and print list of tuples 
tupleList = [("python", "6"), ("JavaScript", "9"), ("C", "2")]
print("The list of tuples before conversion is : " + str(tupleList))

# Converting integer values in list of tuples to float 
conTupList = []
for tup in tupleList:
	convColl = []
	for ele in tup:
		if ele.isalpha():
		    convColl.append(ele)
		else:
		    convColl.append(float(ele))
	conTupList.append((convColl[0],convColl[1]))

# Printing converted List of Tuples 
print("The list of tuples after conversion is : " + str(conTupList))

Output:

The list of tuples before conversion is : [('python', '6'), ('JavaScript', '9'), ('C', '2')]
The list of tuples after conversion is : [('python', 6.0), ('JavaScript', 9.0), ('C', 2.0)]

Approach 2: Using loop, isalpha(), float(), and list comprehension

The same task can be performed using list comprehension which will perform the task in lesser code which is simpler to understand.

Program

# Python program to convert integer values 
# in a list of tuples to float

# creating and print list of tuples 
tupleList = [("python", "6"), ("JavaScript", "9"), ("C", "2")]
print("The list of tuples before conversion is : " + str(tupleList))

# Converting integer values in list of tuples to float 
conTupList = []
for tup in tupleList:
    tupVal = [ele if ele.isalpha() else float(ele) for ele in tup]
    conTupList.append((tupVal[0],tupVal[1]))

# Printing converted List of Tuples 
print("The list of tuples after conversion is : " + str(conTupList))

Output:

The list of tuples before conversion is : [('python', '6'), ('JavaScript', '9'), ('C', '2')]
The list of tuples after conversion is : [('python', 6.0), ('JavaScript', 9.0), ('C', 2.0)]

Python Tuple Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.