How to Convert Between Lists of Floats and Integers in Python
This guide explores the conversion between lists of floats and lists of integers in Python, which is a common task in numerical computing, data processing, and general programming.
We will cover converting floats to integers using type casting, rounding, and the math
module, as well as converting integers to floats. We will demonstrate these techniques with list comprehensions, the map()
function, for loops and NumPy.
Converting a List of Floats to a List of Integers
There are several ways to convert a list of floats to a list of integers, each with slightly different behavior:
Using int()
(Truncation)
The int()
constructor truncates the float, discarding the decimal portion:
list_of_floats = [1.23, 3.45, 5.67]
result = [int(item) for item in list_of_floats]
print(result) # Output: [1, 3, 5]
- The list comprehension applies
int()
on each item of thelist_of_floats
.
Using round()
(Rounding)
The round()
function rounds to the nearest integer:
list_of_floats = [1.23, 3.45, 5.67]
result = [round(item) for item in list_of_floats]
print(result) # Output: [1, 3, 6]
round()
uses standard rounding rules to transform each float to the closest int.
Using math.ceil()
(Rounding Up)
The math.ceil()
function rounds up to the nearest integer and the list comprehension applies the function to each float.
import math
list_of_floats = [1.23, 3.45, 5.67]
result = [math.ceil(item) for item in list_of_floats]
print(result) # Output: [2, 4, 6]
Using math.floor()
(Rounding Down)
The math.floor()
function rounds down to the nearest integer and the list comprehension applies the function to each float.
import math
list_of_floats = [1.23, 3.45, 5.67]
result = [math.floor(item) for item in list_of_floats]
print(result) # Output: [1, 3, 5]
Using map()
The map
function offers an alternative approach to applying int()
, round()
, math.ceil()
and math.floor()
to the list:
list_of_floats = [2.4, 3.5, 6.7, 8.1]
new_list = list(map(round, list_of_floats))
print(new_list) # Output: [2, 4, 7, 8]
- The first argument of
map()
is the function to apply. - The second argument of
map()
is the iterable. - The output is a map object, so the list constructor
list()
is used to transform it to a list.
Converting a List of Integers to a List of Floats
Using a List Comprehension
A list comprehension is concise and efficient for this conversion. In the following example the float()
constructor is applied on each element of the list.
list_of_integers = [3, 5, 7, 9]
new_list = [float(item) for item in list_of_integers]
print(new_list) # Output: [3.0, 5.0, 7.0, 9.0]
Using map()
The map function applies the float()
constructor to each element of the list.
list_of_integers = [3, 5, 7, 9]
new_list = list(map(float, list_of_integers))
print(new_list) # Output: [3.0, 5.0, 7.0, 9.0]
Using a for loop
list_of_integers = [3, 5, 7, 9]
new_list = []
for item in list_of_integers:
new_list.append(float(item))
print(new_list) # Output: [3.0, 5.0, 7.0, 9.0]
Using NumPy
np.array(list_of_integers, dtype=np.float32)
converts the list of ints into a numpy array of floats. In this way, the numpy array of floats is converted back into a list.
import numpy as np
list_of_integers = [3, 5, 7, 9]
new_list = list(np.array(list_of_integers, dtype=np.float32))
print(new_list) # Output: [3.0, 5.0, 7.0, 9.0]
Conclusion
This guide presented multiple ways of converting lists of integers to floats, and lists of floats to integers.
- The
int()
constructor performs truncation, which is suitable in many use cases. round()
rounds the number, andmath.ceil()
andmath.floor()
can be used to round up or down.- For converting a list of integers to float,
float()
can be used, and all of these can be used with themap()
function or with list comprehensions.