Updating this for python3 since other answers leverage python2's map
, which returns a list
, where python3's map
returns an iterator. You can have the list
function consume your map
object:
l = [25.0, 193.0, 281.75, 87.5, 80.5, 449.75, 306.25, 281.75, 87.5, 675.5,986.125, 306.25, 281.75]
list(map(round, l))
[25, 193, 282, 88, 80, 450, 306, 282, 88, 676, 986, 306, 282]
To use round
in this way for a specific n
, you'll want to use functools.partial
:
from functools import partial
n = 3
n_round = partial(round, ndigits=3)
n_round(123.4678)
123.468
new_list = list(map(n_round, list_of_floats))