I have a list of numbers which I need to round into integers before I continue using the list. Example source list:
[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]
What would I do to save this list with all of the numbers rounded to an integer?
You could use the built-in function round()
with a list comprehension:
newlist = [round(x) for x in list]
You could also use the built-in function map()
:
newlist = list(map(round, list))
I wouldn't recommend list
as a name, though, because you are shadowing the built-in type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With