Issue
I have a list like this:
my_list = [1.41421350951, 1.73205688772, 2.44948973178, 2.236067979]
I want to remove some decimals from each index by my own choice. for example, I want every index has just 4 decimals.
like this:
my_new_list = [1.4142, 1.7320, 2.4494, 2.2360]
what library or function or method should I use?
Solution
you can use the built-in function round
with a list comprehension:
my_list = [1.41421350951, 1.73205688772, 2.44948973178, 2.236067979]
[round(e, 4) for e in my_list]
output:
[1.4142, 1.7321, 2.4495, 2.2361]
Answered By - kederrac Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.