0

I have a list which contains values, and I have another list which contains the corresponding number of decimal places that I would like those numbers rounded to.

values = [0.7065099901199341, 128.7489971923828, 1.0810200233459473]
decimalplaces = [5, 3, 5]

so for example i want the first value rounded to 5 decimal places, the second to 3, and the third to 5.

What I would like to get as an output is [0.70650, 128.749, 1.08102].

I have tried the following:

roundedvalues = [round(int(x),int(y)) for x in values for y in decimalplaces]

But instead i'm getting the following:

>> print(roundedvalues)

>>>[0, 0, 0, 128, 128, 128, 1, 1, 1]

What I would like to get as an output is [0.70650, 128.749, 1.08102].

Many thanks in advance for your help!

spmf1694
  • 41
  • 3
  • The problem is with pairing up the `5` with the `0.7065099901199341`, etc. - i.e., iterating over the two lists in parallel. This is **extremely** commonly asked about; please see the linked duplicate. It should be obvious how to proceed from there. – Karl Knechtel Jan 25 '23 at 08:32
  • If you print a list containing the value 0.70650 then this will be output as 0.7065. Also, 0.7065099901199341 rounded to 5 decimal places is 0.70651 – DarkKnight Jan 25 '23 at 08:38

0 Answers0