Convert a List of Floats to a List of Integers in Python

Last updated: Apr 9, 2024
5 min

#Convert a List of Floats to a List of Integers in Python

To convert a list of floats to a list of integers:

1. Use a list comprehension to iterate over the list.
2. Use the `int()` class to convert the current float to an integer.
3. The new list will only contain integer values.
main.py
```Copied!```list_of_floats = [1.23, 3.45, 5.67]

result = [int(item) for item in list_of_floats]
print(result)  # ๐๏ธ [1, 3, 5]
``````

We used a list comprehension to iterate over the list of floats.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we pass the current float value to the int() class to convert it to an integer.

The new list only contains integer values.

#Convert a List of Floats to a List of Integers using round()

You can also use the `round()` function to round each float to the nearest integer.

main.py
```Copied!```list_of_floats = [1.23, 3.45, 5.67]

result = [round(item) for item in list_of_floats]
print(result)  # ๐๏ธ [1, 3, 6]
``````

Instead of converting to an integer by dropping the decimal, this example converts the floating-point numbers in the list by rounding them to the nearest integers.

The round() function takes the following 2 parameters:

NameDescription
`number`the number to round to `ndigits` precision after the decimal
`ndigits`the number of digits after the decimal, the number should have after the operation (optional)

The `round` function returns the number rounded to `ndigits` precision after the decimal point.

If `ndigits` is omitted, the function returns the nearest integer.

main.py
```Copied!```print(round(3.45))  # ๐๏ธ 3
print(round(3.55))  # ๐๏ธ 4
``````

#Convert a List of Floats to a List of Integers using math.ceil

If you need to round up, use the `math.ceil()` method.

main.py
```Copied!```import math

list_of_floats = [1.23, 3.45, 5.67]

result = [math.ceil(item) for item in list_of_floats]
print(result)  # ๐๏ธ [2, 4, 6]
``````

The math.ceil() method returns the smallest integer greater than or equal to the provided number.

main.py
```Copied!```print(math.ceil(3.01)) # ๐๏ธ 4
print(math.ceil(3.99)) # ๐๏ธ 4
``````

#Convert a List of Floats to a List of Integers using math.floor

If you need to round down, use the `math.floor()` method.

main.py
```Copied!```import math

list_of_floats = [1.23, 3.45, 5.67]

result_4 = [math.floor(item) for item in list_of_floats]
print(result_4)  # ๐๏ธ [1, 3, 5]
``````

The math.floor method returns the largest integer less than or equal to the provided number.

main.py
```Copied!```print(math.floor(4.999))  # ๐๏ธ 4
print(math.floor(4.001))  # ๐๏ธ 4
``````

Alternatively, you can use the `map()` function.

#Convert a List of Floats to a List of Integers using `map()`

This is a three-step process:

1. Pass the `round()` function and the list to the `map()` function.
2. The `map()` function will pass each float to the `round()` function.
3. Use the `list()` class to convert the `map` object to a list.
main.py
```Copied!```list_of_floats = [2.4, 3.5, 6.7, 8.1]

new_list = list(map(round, list_of_floats))

# ๐๏ธ [2, 4, 7, 8]
print(new_list)
``````

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The `map()` function calls the `round()` function will each floating-point number in the list and rounds each value.

The last step is to use the `list()` class to convert the `map` object to a list.

The `list()` class takes an iterable and returns a list object.

Which approach you pick is a matter of personal preference. I'd go with the list comprehension as I find it more direct and easier to read.

#Convert a List of Integers to a List of Floats in Python

To convert a list of integers to a list of floats:

1. Use a list comprehension to iterate over the list.
2. Use the `float()` class to convert each integer to a float.
3. The new list will only contain floating-point numbers.
main.py
```Copied!```list_of_integers = [3, 5, 7, 9]

new_list = [float(item) for item in list_of_integers]

# ๐๏ธ [3.0, 5.0, 7.0, 9.0]
print(new_list)
``````

We used a list comprehension to iterate over the list of integers.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we pass the integer to the float() class to convert it to a floating-point number.

The new list will only contain float values.

Alternatively, you can use the `map()` function.

#Convert a List of Integers to a List of Floats using map()

This is a three-step process:

1. Pass the `float()` class and the list to the `map()` function.
2. The `map()` function will call the `float()` class with each item in the list.
3. Use the `list()` class to convert the `map` object to a list.
main.py
```Copied!```list_of_integers = [3, 5, 7, 9]

new_list = list(map(float, list_of_integers))

# ๐๏ธ [3.0, 5.0, 7.0, 9.0]
print(new_list)
``````

The `map()` function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The `float()` class gets passed each integer in the list and converts each value to a floating-point number.

Lastly, we used the `list()` class to convert the `map` object to a list.

The `list()` class takes an iterable and returns a list object.

#Convert a List of Integers to a List of Floats using a `for` loop

You can also use a for loop to convert a list of integers to a list of floats.

main.py
```Copied!```list_of_integers = [3, 5, 7, 9]

new_list = []

for item in list_of_integers:
new_list.append(float(item))

print(new_list) # ๐๏ธ [3.0, 5.0, 7.0, 9.0]
``````

On each iteration of the `for` loop, we convert the current item to a floating-point number and append the result to a new list.

#Convert a List of Integers to a List of Floats using NumPy

This is a two-step process:

1. Pass the list and the float data type to the `numpy.array()` method.
2. Optionally convert the NumPy array to a list.
main.py
```Copied!```import numpy as np

list_of_integers = [3, 5, 7, 9]

new_list = list(
np.array(list_of_integers, dtype=np.float32)
)

# ๐๏ธ [3.0, 5.0, 7.0, 9.0]
print(new_list)
``````

You can install NumPy by running the following command.

shell
```Copied!```pip install numpy

# ๐๏ธ or with pip3
pip3 install numpy
``````

The numpy.array method creates an array from the provided object.

We set the `dtype` argument to `np.float32` to specify the desired data type for the elements in the array.

The last (optional) step is to use the `list()` class to convert the NumPy array to a list object.

The `list()` class takes an iterable and returns a list object.