Convert List from Integer to Float in Python (2 Examples)
In this article you’ll learn how to transform lists of integers to lists of floats in the Python programming language.
The tutorial will consist of the following content:
If you want to learn more about these topics, keep reading!
The following data, which is a list of integers, will be used as a basis for this Python tutorial. See how it is created and printed below.
sl_int=[3, 2, 4, -20, -5, 120] # create sample data print(sl_int) # print sample data # [3, 2, 4, -20, -5, 120]
Now it is time to check the data class of the elements of sl_int.
for element in sl_int: # print sample data type print(type(element)) # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'> # <class 'int'>
As seen, all elements have the data type integer. In the following sections, you will see how to convert list elements from integers to floats in two different ways.
Example 1: Transform List of Integers to Floats Using list() & map() Functions
In this example, I’ll demonstrate how to apply the list() and map() functions to change the data type from integer to float in a Python list, see the script below.
sl_flt1=list(map(float, sl_int)) # apply list() & map() functions print(sl_flt1) # print output of list() & map() # [3.0, 2.0, 4.0, -20.0, -5.0, 120.0]
Based on the previous output, you can already see a difference compared to our input data: The values are now displayed with a 0 on the right side of the decimal point, which indicates that we are now dealing with floats.
However, let’s check in a for loop if the data class was converted properly.
for element in sl_flt1: # print converted data type print(type(element)) # <class 'float'> # <class 'float'> # <class 'float'> # <class 'float'> # <class 'float'> # <class 'float'>
You can see how all elements in sl_flt1 are in the float class. Looks like it worked well!
Example 2: Transform List of Integers to Floats Using List Comprehension
Example 2 demonstrates how to transform a list of integers into a list of floats with the help of list comprehension.
sl_flt2=[float(x) for x in sl_int] # list comprehension method print(sl_flt2) # print output of list comprehension # [3.0, 2.0, 4.0, -20.0, -5.0, 120.0]
Let’s see if the list comprehension has worked.
for element in sl_flt2: # print converted data type print(type(element)) # <class 'float'> # <class 'float'> # <class 'float'> # <class 'float'> # <class 'float'> # <class 'float'>
Well done, it works as it is supposed to.
Video & Further Resources
I have recently released a video on my YouTube channel, which shows the Python programming codes of this page. Please find the video below:
The YouTube video will be added soon.
Also, you may read the other Python articles at https://statisticsglobe.com/.
- Convert Float to Integer in pandas DataFrame Column in Python
- Convert Integer to Float in pandas DataFrame Column in Python
- Introduction to Python
At this point, you should know how to convert integers in lists to floats in Python. Don’t hesitate to let me know in the comments if you have additional questions and/or comments.
This page was created in collaboration with Cansu Kebabci. Have a look at Cansu’s author page to get more information about her professional background, a list of all his tutorials, as well as an overview of her other tasks on Statistics Globe.
Statistics Globe Newsletter