Python developers often work with lists of data, and sometimes it’s necessary to convert a list of floating-point numbers (floats) into integers. For example, you may start with a list like [1.9, 2.5, 3.7]
and want to convert it to [1, 2, 3]
. This article will guide you through the top methods for performing this conversion efficiently and accurately.
Method 1: Using List Comprehension with int()
List comprehension in Python offers a concise way to create lists. By applying int()
inside a list comprehension, each float is cast to an integer, effectively truncating any decimal parts.
Here’s an example:
float_list = [1.9, 2.5, 3.7] int_list = [int(num) for num in float_list]
Output: [1, 2, 3]
This snippet iterates over each element in the list of floats float_list
, applies the int()
function, which truncates the float by removing any fractional parts, and accumulates the converted integers into a new list called int_list
.
Method 2: Using map() with int()
The map()
function applies a specified function to each item of an iterable (like a list) and returns an iterator. Combined with int()
, it allows efficient casting of each float to an integer without an explicit loop.
Here’s an example:
float_list = [1.9, 2.5, 3.7] int_list = list(map(int, float_list))
Output: [1, 2, 3]
The map()
function takes int
as the function argument and applies it to each element of float_list
. The result must be explicitly converted to a list since map()
returns an iterator.
Method 3: Using numpy.array().astype()
The NumPy library provides a method astype()
, which converts an array to a specified type. This method can handle large datasets efficiently.
Here’s an example:
import numpy as np float_list = [1.9, 2.5, 3.7] int_array = np.array(float_list).astype(int)
Output: array([1, 2, 3])
The code first converts the list of floats into a NumPy array, then uses astype(int)
to create a new array of integers. Note that the output is a NumPy array, not a list.
Method 4: Using Math Module for Rounding
While the int()
casting method simply truncates the decimal, you can also round the floats before converting to integers using the Python Math module’s math.floor()
to always round down, or math.ceil()
to always round up.
Here’s an example:
import math float_list = [1.9, 2.5, 3.7] int_list_floor = [math.floor(num) for num in float_list] int_list_ceil = [math.ceil(num) for num in float_list]
Output for floor: [1, 2, 3]
Output for ceil: [2, 3, 4]
The code demonstrates two approaches – using math.floor()
to round down, and math.ceil()
to round up. List comprehension is used for iterating over the float list and applying the rounding method.
Bonus One-Liner Method 5: Using round()
To round floats to the nearest integer before conversion, you can use the round()
function inside a list comprehension. It follows the standard rounding rules, rounding half up.
Here’s an example:
float_list = [1.9, 2.5, 3.7] int_list = [round(num) for num in float_list]
Output: [2, 3, 4]
This snippet takes each element from float_list
, rounds it to the nearest integer using round()
, and builds a new integer list int_list
. Norably, 2.5
is rounded up to 3
.
Summary/Discussion
- Method 1: List Comprehension with int(). Straightforward and readable. However, it only truncates the decimal part without actual rounding.
- Method 2: map() with int(). Clean and functional style code. Returns an iterator which must be converted to a list explicitly.
- Method 3: numpy.array().astype(). Best for large datasets and numerical processing. Introduces dependency on NumPy library.
- Method 4: Math Module for Rounding. Offers control over rounding direction (up or down). Requires importing the math module.
- Method 5: Using round(). Rounds floats to the nearest integer, following half-up rule. Simple one-liner solution for standard rounding.