Pretty print a list of floats in Python

avatar

Borislav Hadzhiev

Last updated: Sep 1, 2022

banner

Photo from Unsplash

Pretty print a list of floats in Python #

To pretty print a list of floats:

  1. Use a list comprehension to iterate over the list.
  2. Use a formatted string literal to format each float to a specified number of decimal places.
  3. Use the print() function to print the result.
main.py
list_of_floats = [3.596821, 2.423287, 5.9283237, 7.23894823] # ✅ pretty print list of floats formatted to 2 decimals result = [f'{item:.2f}' for item in list_of_floats] print(result) # 👉️ ['3.60', '2.42', '5.93', '7.24'] # --------------------------------------------------------- # ✅ pretty print list of floats formatted to 3 decimals result = [f'{item:.3f}' for item in list_of_floats] print(result) # 👉️ ['3.597', '2.423', '5.928', '7.239'] # --------------------------------------------------------- # ✅ pretty print list of floats without comma between elements result = [f'{item:.2f}' for item in list_of_floats] print(str(result).replace(',', '')) # 👉️ ['3.60' '2.42' '5.93' '7.24'] print(' '.join(result)) # 👉️ 3.60 2.42 5.93 7.24

The first step is to use a list comprehension to iterate over the list of floating-point numbers.

main.py
list_of_floats = [3.596821, 2.423287, 5.9283237, 7.23894823] result = [f'{item:.2f}' for item in list_of_floats] print(result) # 👉️ ['3.60', '2.42', '5.93', '7.24']

On each iteration, we use a formatted string literal to format the float to 2 decimal places and return the result.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

Make sure to wrap expressions in curly braces - {expression}.

Formatted string literals also enable us to use the format-specific mini-language in expression blocks.

main.py
num = 3.596821 print(f'{num:.3f}') # 👉️ '3.597' print(f'{num:.4f}') # 👉️ '3.5968'
The digit after the period is the number of decimal places the float should have.

If you have the number of decimal places stored in a variable, wrap it in curly braces in the f-string.

main.py
list_of_floats = [3.596821, 2.423287, 5.9283237, 7.23894823] decimal_places = 2 result = [f'{item:.{decimal_places}f}' for item in list_of_floats] print(result) # 👉️ ['3.60', '2.42', '5.93', '7.24']

If you need to format the list of floats to a specific number of digits after the decimal and print them without comma separators, use the str.replace() method.

main.py
list_of_floats = [3.596821, 2.423287, 5.9283237, 7.23894823] result = [f'{item:.2f}' for item in list_of_floats] print(str(result).replace(',', '')) # 👉️ ['3.60' '2.42' '5.93' '7.24']

We converted the list to a string and used the str.replace() method to remove the commas.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

You can use the str.join() method if you need to print the list of floats without square brackets and commas.

main.py
list_of_floats = [3.596821, 2.423287, 5.9283237, 7.23894823] result = [f'{item:.2f}' for item in list_of_floats] print(' '.join(result)) # 👉️ 3.60 2.42 5.93 7.24

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

main.py
list_of_floats = [3.596821, 2.423287, 5.9283237, 7.23894823] result = [f'{item:.2f}' for item in list_of_floats] print(', '.join(result)) # 👉️ 3.60, 2.42, 5.93, 7.24
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.