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)]
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:
Output:
The same task can be performed using list comprehension which will perform the task in lesser code which is simpler to understand.
Program:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer