shorten all floats in a list 1

shorten all floats in a list

my_list = [1.000, 2.41, 2.5566778]
my_rounded_list = [ round(elem, 2) for elem in my_list ]
my_rounded_list == [1.00, 2.41, 2.56]

Here is what the above code is Doing:
1. We create a list of numbers.
2. We create a new list, my_rounded_list, which is the result of rounding each element in my_list to 2 decimal places.
3. We check that the result is what we expect.

Similar Posts