Q:

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

belongs to collection: Python Tuple Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to convert set into tuple and tuple... >>
<< Python program to change the sign of elements of t...