Convert List from Character String to Float in Python (3 Examples)
Hi! This tutorial will show you 3 simple ways to convert a list of character strings to float numbers in the Python programming language.
First, though, here is an overview of this tutorial:
Let’s dive into Python code!
Create Sample List
Here, we will create a sample list of character strings that we will be converted into float numbers. In your preferred Python IDE, run the line of code below.
string_list = ["1.2", "3.4", "5.6"]
As seen in the script above, there are three strings in list string_list.
Example 1: Transform List of Strings to List of Floats via map() Function
In this first example, we will use the map()
function to iterate through string_list and replace the strings with float numbers, which results in a new list called float_list. After the implementation, we will test the data types of elements in float_list via the type() function inside a for loop.
float_list = list(map(float, string_list)) print(float_list) # [1.2, 3.4, 5.6] for element in float_list: print(type(element)) # <class 'float'> # <class 'float'> # <class 'float'>
As seen above, all elements are float numbers as desired.
Example 2: Transform List of Strings to List of Floats via float() Function & for Loop
In this second example, we will use a for loop to loop the list through the float()
function, which will convert the list of strings to float data type.
float_list = [] for i in string_list: float_list.append(float(i)) print(float_list) # [1.2, 3.4, 5.6] for element in float_list: print(type(element)) # <class 'float'> # <class 'float'> # <class 'float'>
Well done! All list items are in float type, like in Example 1.
Example 3: Transform List of Strings to List of Floats via List Comprehension
In this third and final example, we will use list comprehension to convert the list of character strings to floats. This method works as well as the previous methods as shown in the following example.
float_list = [float(item) for item in string_list] print(float_list) # [1.2, 3.4, 5.6] for element in float_list: print(type(element)) # <class 'float'> # <class 'float'> # <class 'float'>
So, that is how to convert a list of character strings to floats in the Python programming language. I hope you found this tutorial helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a list of character strings to float in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a list of character strings to float in Python.
The YouTube video will be added soon.
Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Sort List of datetime Objects in Python (Example)
- Check if String Exists in List in Python (3 Examples)
- Count Duplicates in List in Python (2 Examples)
- Access List Element by Index in Python (3 Examples)
- Learn Python Programming
This post has shown how to convert a list of character strings to float numbers in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter