Depends on what you mean by "round off the list items".
If you want only print the rounded values, make a loop by the elements and prints them:
for n in lista:
print(f'{n:.2f}')
In that case, as the idea is only print, you wouldn’t need to create another list, as suggested in another answer (it would only make sense to create another list if you were to use it for other things later, otherwise you would be creating it for nothing).
Of course you can do some "twitches":
for i, n in enumerate(lista, start=1):
print(f'{i}º elemento = {n:.2f}')
Printing:
1º elemento = 2.23
2º elemento = 2.35
3º elemento = 2.46
But anyway, once you go through the list, you can do whatever you want with each element.
But if "round off the list items" means to create another list with rounded values, then you can do:
rounded = [ round(n, 2) for n in lista ]
I used round
instead of f-string, because if I do f'{n:.2f}'
the result will be a string. But if you want a list of numbers, you must use round
. Of course, printing won’t make a difference because the values will be displayed the same way, but if you’re going to use this list for something later (like making calculations), you’d better create it with numbers instead of strings.
Remembering that in this case, as the numbers have already been rounded, just print them directly:
for n in rounded:
print(n)
Finally (although not the focus of the question), read the documentation on floating point number limitations, to understand, among other things, why round(2.675, 2)
is 2.67
and not 2.68
.