TypeError: unsupported format string passed to list.__format__

Viewed 95053

I have two list named results and p_results. I want to show those list values in table like if

results = [1,2,3,4]
p_results = [5,6,7,8]

I want something like this

1     5
2     6
3     7
4     8

        print('{:3}{:20}'.format(results, p_results))

running the code:

runfile('D:/4/2d.py', wdir='D:/4')
Traceback (most recent call last):

  File "<ipython-input-59-1abb0c96f0c0>", line 1, in <module>
    runfile('D:/4/2d.py', wdir='D:/4')

  File "C:\Users\Rabinsen\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\Rabinsen\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "D:/4/2d.py", line 61, in <module>
    print('{:3}{:20}'.format(results, p_results))

TypeError: unsupported format string passed to list.__format__
6 Answers

One trick is to solve the spacing part:

'{:15}'.format('{}'.format([1,2,3]))

For iterating over two lists:

[print('res:', i, ': p_res', j) for i, j in zip(results, p_results)]
res: 1 : p_res 5
res: 2 : p_res 6
res: 3 : p_res 7
res: 4 : p_res 8

Combining formatting and zipping gives you what you wanted to achieve.

You can pass a list type to format (using just {}), but the formatting you requested isn't available. And the standard formatting isn't suited anyway, so...

What you want is each list on one separate column. You'll have to zip lists together, and iterate on the result to pass it to format:

results = [1,2,3,4]
p_results = [5,6,7,8]

for result,p_result in zip(results,p_results):
    print('{:3}{:20}'.format(result,p_result))

That prints something like:

  1                   5
  2                   6
  3                   7
  4                   8

if you still wish to do it with a single line, write:

results = [1,2,3,4]
p_results = [5,6,7,8]

[print("{:3}{:20}".format(*x)) for x in np.c_[results,p_results]][0]

That prints:

  1                   5
  2                   6
  3                   7
  4                   8

To be sure, np is for NumPy

Based on @istvan solution, just wrap your list with str:

f'{str(my_list):15}{str(my_other_list):25}'

(Or if you stick to @istvan example:)

'{:15}'.format(str([1,2,3]))
# or
f'{str([1,2,3]):15}'

Did you try print('{},{}'.format(results[:3], p_results[:20]))

The way you structured your print like looks like a combination of two styles,

print(f'{results[:3]}, {p_results[:20]}')

and

print('{}, {}'.format(results[:3], p_results[:20]))

Your attempt is

print('{:3}, {:20}'.format(results, p_results))

This is the root of your error, here in example reproducing same error:

>>> a = [1, 2, 3]
>>> print('{:1}'.format(a))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported format string passed to list.__format__

>>> print('{}'.format(a[:1]))
[1]

I would recommend that you use Pandas library for that purpose

import pandas as pd
results = [1,2,3,4]
p_results = [5,6,7,8]
d = {'column-1':results, 'column-2':p_results}
data_frame = pd.DataFrame(data=d)
print(data_frame)
   column-1  column-2
0         1         5
1         2         6
2         3         7
3         4         8

Notice that you have an index using data frame object

Also you can make new columns an operations, like for instance let us create a third column to store the product of column-1 and column-2

data_frame['product'] = data_frame['column-1']*data_frame['column-2']

The result you get now printing is:

   column-1  column-2  product
0         1         5        5
1         2         6       12
2         3         7       21
3         4         8       32

Hope this helps! Happy coding!

Related