-2

I have the following list of values:

      value_list=[2.5655665, 3.151498745, 3.1, 0.9999999999]

I need to update this list keeping only to the second decimal place. I would like the result to be:

      print(value_list)

      [2.56, 3.15, 3.1, 0.99]

I tried to keep only to the second decimal place using the round() method passing parameter 2. As follows:

     value_list.round(2)

But, the error message appears:

     AttributeError: 'list' object has no attribute 'round'

I made an attempt by transforming the value_list to the array type, like this:

     import numpy as np
     value_list = np.array(value_list).round(2)

This way it works, but it returns an array and I needed the return to be of type list. How can I return a list with only up to the second decimal place?

Jane Borges
  • 552
  • 5
  • 14
  • 1
    If you want to keep the values as floats, there's no way to keep only a specific number of decimal places. You can *round* float values, but the nearest rounded float value may still contain more decimal places than you like. You'd need to convert them to other data types, like a string or `Decimal`, to have a say about the *exact* formatting. – deceze Aug 06 '21 at 13:02
  • Most math and element-wise functions have to be applied one by one to lists. I like list comprehensions, but `map` also works. Or for chains of operations consider generators and `itertools`. There are only 11 list methods, so it's not hard to remember them. – hpaulj Aug 06 '21 at 15:55

4 Answers4

1

You can use map and apply whatever function you see fit:

value_list=[2.5655665, 3.151498745, 3.1, 0.9999999999]
value_rounded = list(map(lambda x: float(format(x, '.2f')), value_list))
value_truncated = list(map(lambda x: float(str(x)[:str(x).index('.')+3]), value_list))
print(value_rounded)
print(value_truncated)

Outputs for both cases:

[2.57, 3.15, 3.1, 1.0]
[2.56, 3.15, 3.1, 0.99]
Tranbi
  • 11,407
  • 6
  • 16
  • 33
0
value_list=[2.5655665, 3.151498745, 3.1, 0.9999999999]
my_formatted_list = [ '%.2f' % elem for elem in value_list ]
phuycke
  • 346
  • 2
  • 10
0

Floating point values are not necessarily represented as rounded but you can print them this way:

value_list = [2.5655665, 3.151498745, 3.1, 0.9999999999]
for v in value_list:
   print("{:.2f}".format(v))

You can also use Decimal type to ensure the values are rounded: How can I format a decimal to always show 2 decimal places?

Then, you can create a list of values rounded to the second decimal place:

value_list = [2.5655665, 3.151498745, 3.1, 0.9999999999]
values_rounded = []
for v in value_list:
   values_rounded.append(v.round(2))
sophros
  • 14,672
  • 11
  • 46
  • 75
0

Maybe you want to use Decimal. First you need to set the precision in the context:

from decimal import getcontext
getcontext().prec=2

Then convert:

value_list = [Decimal(i) for i in value_list]

If you print the list, you will still see all the other digits:

[Decimal('2.56556650000000008304823495564050972461700439453125'),
 Decimal('3.151498745000000045735077947028912603855133056640625'),
 Decimal('3.100000000000000088817841970012523233890533447265625'),
 Decimal('0.9999999998999999917259629000909626483917236328125')]

However, for mathematical operations, your precision will be respected; e.g.

[i -1 for i in value_list]

returns

[Decimal('1.6'), Decimal('2.2'), Decimal('2.1'), Decimal('-1.0E-10')]
suvayu
  • 4,271
  • 2
  • 29
  • 35