PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How to remove some decimal points in a list?

 August 10, 2022     decimal, list, methods, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing