ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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?
Submitted by Shivang Yadav, on July 18, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered set of values enclosed in square brackets [].

Example:

list = [3 ,1,  5, 7]

A list of tuples is a list whose each element is a tuple.

Example:

upList = [("python", 7), ("learn" , 1), ("programming", 7), ("code" , 3)]

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,

Method 1:

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)]

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 »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.