Python format a list of numbers

I want to print a list of numbers, but I want to format each member of the list before it is printed. For example,

theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]

I want the following output printed given the above list as an input:

[1.34, 7.42, 6.97, 4.55]

For any one member of the list, I know I can format it by using

print "%.2f" % member

Is there a command/function that can do this for the whole list? I can write one, but was wondering if one already exists.

Python format a list of numbers

asked May 4, 2010 at 0:22

Curious2learnCurious2learn

29.8k41 gold badges104 silver badges124 bronze badges

If you just want to print the numbers you can use a simple loop:

for member in theList:
    print "%.2f" % member

If you want to store the result for later you can use a list comprehension:

formattedList = ["%.2f" % member for member in theList]

You can then print this list to get the output as in your question:

print formattedList

Note also that % is being deprecated. If you are using Python 2.6 or newer prefer to use format.

answered May 4, 2010 at 0:24

Mark ByersMark Byers

781k184 gold badges1551 silver badges1440 bronze badges

1

For Python 3.5.1, you can use:

>>> theList = [1.343465432, 7.423334343, 6.967997797, 4.5522577]
>>> strFormat = len(theList) * '{:10f} '
>>> formattedList = strFormat.format(*theList)
>>> print(formattedList)

The result is:

'  1.343465   7.423334   6.967998   4.552258 '

Michael

7,6255 gold badges57 silver badges87 bronze badges

answered Mar 31, 2016 at 20:16

1

A very short solution using "".format() and a generator expression:

>>> theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]

>>> print(['{:.2f}'.format(item) for item in theList])

['1.34', '7.42', '6.97', '4.55']

answered Jul 5, 2017 at 9:29

Python format a list of numbers

You can use list comprehension, join and some string manipulation, as follows:

>>> theList=[1.343465432, 7.423334343, 6.967997797, 4.5522577]
>>> def format(l):
...     return "["+", ".join(["%.2f" % x for x in l])+"]"
... 
>>> format(theList)
'[1.34, 7.42, 6.97, 4.55]'

answered May 4, 2010 at 0:32

moshezmoshez

34.7k1 gold badge20 silver badges14 bronze badges

You can use the map function

l2 = map(lambda n: "%.2f" % n, l)

answered May 4, 2010 at 0:39

jbochijbochi

28.2k16 gold badges71 silver badges90 bronze badges

Try this one if you don't need to save your values:

list = [0.34555, 0.2323456, 0.6234232, 0.45234234]
for member in list:
    form='{:.1%}'.format(member)
    print(form)

output:

34.6%
23.2%
62.3%
45.2%

answered Jun 4, 2018 at 9:48

How do you format a list element in Python?

Given a List of float values, the task is to truncate all float values to 2-decimal digit. Let's see the different methods to do the task. Method #1 : Using List comprehension..
Method #2 : Using Map..
Method #3 : Using format..
Method #4 : Using Iteration..

What is %d and %s in Python?

%s is used as a placeholder for string values you want to inject into a formatted string. %d is used as a placeholder for numeric or decimal values.

What does %D in Python do?

What does %d do in Python? The %d operator is used as a placeholder to specify integer values, decimals, or numbers. It allows us to print numbers within strings or other values. The %d operator is put where the integer is to be specified.

How do I format a list to a string in Python?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.