Python round list to 2 decimal places
I have a list which consists of float values but they're too detailed to proceed. I know we can shorten them by using the Show
My question is how can I turn a list of values into their rounded equivalents without using an iterator. I've tried something, but it throws a
How can I provide a clean list like:
gandreadis 2,8472 gold badges27 silver badges37 bronze badges asked Mar 16, 2011 at 13:37
1
returns:
Also, don't call your variable If you want to obtain
returns:
gandreadis 2,8472 gold badges27 silver badges37 bronze badges answered Mar 16, 2011 at 13:39
eumiroeumiro 198k33 gold badges294 silver badges259 bronze badges 4 If you really want an iterator-free solution, you can use numpy and its array round function.
answered Mar 25, 2018 at 4:37
john ktejikjohn ktejik 5,6674 gold badges48 silver badges53 bronze badges 2 You
might want to look at Python's
answered Mar 16, 2011 at 14:08
martineaumartineau 115k25 gold badges160 silver badges282 bronze badges 0
pzaenger 9,8513 gold badges39 silver badges44 bronze badges answered Oct 21, 2018 at 17:42
Silvia42Silvia42 811 silver badge1 bronze badge You can use the built-in
Alternatively you could also create a named function for the rounding up to a specific digit using
answered Sep 8, 2020 at 18:22
jojojojo 9,4182 gold badges48 silver badges69 bronze badges Another option which doesn't require numpy is:
answered May 16, 2018 at 15:55
Easiest way : import numpy as np np.round(list,2) answered Jul 4 at 14:42
1 How do you round to 2 decimal places in Python?Python's round() function requires two arguments. First is the number to be rounded. Second argument decides the number of decimal places to which it is rounded. To round the number to 2 decimals, give second argument as 2.
How do you round a list in Python?Use numpy.. a_list = [1.234, 2.345, 3.45, 1.45]. np_array = np. array(a_list). np_round_to_tenths = np. around(np_array, 1). round_to_tenths = list(np_round_to_tenths). print(round_to_tenths). How do you round off values in a list?The round function returns the number rounded to ndigits precision after the decimal point.. Use a list comprehension to iterate over the list.. On each iteration, pass the list element to the round() function.. The new list will contain rounded numbers.. |