0
-------- Final Receipt --------
Milk, 2 Litres $ 2
Potatoes $ 7.5
Sugar $ 3.5

I want this print statement to be

 -------- Final Receipt --------
Milk, 2 Litres    $ 2
Potatoes          $ 7.5
Sugar             $ 3.5

here is my print stament

for mydata in zip(*list(product_in_basket.values())[:-1]):
        print(mydata[1],'$',mydata[2])

and product_in_list is nested dictionary

the code is fully working but need my output style

Nabin KC
  • 21
  • 4
  • 2
    Possible duplicate of [Printing Lists as Tabular Data](https://stackoverflow.com/questions/9535954/printing-lists-as-tabular-data) – NoorJafri May 07 '18 at 10:54
  • Possible duplicate of [How can I fill out a Python string with spaces?](https://stackoverflow.com/questions/5676646/how-can-i-fill-out-a-python-string-with-spaces) – Ohad Eytan May 07 '18 at 10:55

2 Answers2

0

You can use ljust:

data = [['-------- Final Receipt --------'], ['Milk, 2 Litres', '$ 2'], ['Potatoes', '$ 7.5'], ['Sugar', '$ 3.5']]
v, _ = max(data[1:], key=lambda x:len(x[0]))
final_data = data[0][0]+'\n'+'\n'.join(a.ljust(len(v)+3)+b for a, b in data[1:])

Output:

-------- Final Receipt --------
Milk, 2 Litres   $ 2
Potatoes         $ 7.5
Sugar            $ 3.5
Ajax1234
  • 58,711
  • 7
  • 46
  • 83
0

You probably want to fill out the strings with spaces using ljust. So that each column is a certain width.

header = " -------- Final Receipt --------"

l = [["Milk, 2 Litres", 2], ["Potatoes", 7.5], ["Sugar", 3.5]]

print(header)
for mydata in l:
    price = "$ {0}".format(mydata[1])
    print("{0}{1}".format(mydata[0].ljust(16),price.ljust(16)))

I did len(header) to find out that your header was 32 characters, so each column takes up half of that now (16).

See: How can I fill out a Python string with spaces?

result:

 -------- Final Receipt --------
Milk, 2 Litres  $ 2
Potatoes        $ 7.5
Sugar           $ 3.5
Zhenhir
  • 742
  • 4
  • 11