Convert a list of floats to a list of integers in Python

avatar

Borislav Hadzhiev

Last updated: Jul 12, 2022

banner

Photo from Unsplash

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
import math list_of_floats = [1.23, 3.45, 5.67] # ✅ convert list of floats to list of integers # by dropping decimal result_1 = [int(item) for item in list_of_floats] print(result_1) # 👉️ [1, 3, 5] # ----------------------------------------------- # ✅ convert list of floats to list of integers # by rounding to nearest integer result_2 = [round(item) for item in list_of_floats] print(result_2) # 👉️ [1, 3, 6] # ----------------------------------------------- # ✅ convert list of floats to list of integers # by rounding up result_3 = [math.ceil(item) for item in list_of_floats] print(result_3) # 👉️ [2, 4, 6] # ----------------------------------------------- # ✅ convert list of floats to list of integers # by rounding down result_4 = [math.floor(item) for item in list_of_floats] print(result_4) # 👉️ [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.

main.py
list_of_floats = [1.23, 3.45, 5.67] result_1 = [int(item) for item in list_of_floats] print(result_1) # 👉️ [1, 3, 5]

The new list only contains integer values.

Convert a list of floats to a list of integers using round() #

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

  1. Use a list comprehension to iterate over the list.
  2. Use the round() function to round each float to the nearest integer.
  3. The new list will only contain integers.
main.py
list_of_floats = [1.23, 3.45, 5.67] result_2 = [round(item) for item in list_of_floats] print(result_2) # 👉️ [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
numberthe number to round to ndigits precision after the decimal
ndigitsthe 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
print(round(3.45)) # 👉️ 3 print(round(3.55)) # 👉️ 4

Convert a list of floats to list of integers using math.ceil #

To convert a list of floats to a list of integers by rounding up:

  1. Use a list comprehension to iterate over the list.
  2. Use the math.ceil() function to round each float up.
  3. The new list will only contain integers.
main.py
import math list_of_floats = [1.23, 3.45, 5.67] result_3 = [math.ceil(item) for item in list_of_floats] print(result_3) # 👉️ [2, 4, 6]

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

main.py
print(math.ceil(3.01)) # 👉️ 4 print(math.ceil(3.99)) # 👉️ 4

Convert a list of floats to list of integers using math.floor #

To convert a list of floats to a list of integers by rounding down:

  1. Use a list comprehension to iterate over the list.
  2. Use the math.floor() function to round each float down.
  3. The new list will only contain integers.
main.py
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
print(math.floor(4.999)) # 👉️ 4 print(math.floor(4.001)) # 👉️ 4
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.