Convert List from Float to Integer in Python (3 Examples)

 

Hi! This tutorial will show you 3 simple ways to convert a list of floats to integers in the Python programming language.

First, though, here is an overview of this tutorial:

Let’s dive into Python code!

 

Create List of Floats

Here, we will create a list of floats that will be converted to integers in this tutorial. In your preferred Python IDE, run the line of code below.

float_list = [1.2, 3.4, 5.6]

As seen, the list of floats was created by concatenating three decimals in a square bracket and named as float_list.

 

Example 1: Convert List from Float to Integer using List Comprehension

In this first example, we will use a list comprehension and the int() function to convert the list of floats to integers.

int_list = [int(item) for item in float_list]
print(int_list)
 
 
# [1, 3, 5]

As seen, the new list, int_list, contains three integers transformed from the floats in float_list.

 

Example 2: Convert List from Float to Integer using map() Function

In this second example, we will use the map() function to convert the list of floats to integers.

int_list = list(map(int, float_list))
print(int_list)
 
# [1, 3, 5]

We again got the corresponding integers to the floats in float_list. Great!

 

Example 3: Convert List from Float to Integer using NumPy Array

In this third and final example, we will use Python’s NumPy library to convert the list of floats to integers. First, though, we will need to install and import NumPy.

# install numpy
pip install numpy
 
# import numpy
import numpy as np

Next, we will use np.array() function to convert the list of floats to integer.

int_list = np.array(float_list).astype(int).tolist()
print(int_list)
 
 
# [1, 3, 5]

In the astype() function, we specified that we wanted it to be converted to integers, and then we chained the tolist() function to convert the array to a list.

So, that is how to convert a list of floats to integers 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 floats to integers 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 floats to integers in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to convert a list of floats to integers in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top