1

These are the three lists I have:

# made up data
products = ['apple','banana','orange'] 
prices = ['£0.11','£0.07','£0.05']
dates = ['02/04/2017','14/09/2018','06/08/2016']

Important to know

  • The data in these lists will vary along with its size, although they will maintain the same data type.
  • The first elements of each list are linked, likewise for the second and third element etc...

Desired command line interface:

Product | Price | Date of Purchase
--------|-------|------------------
 apple  | £0.11 |    02/04/2017
--------|-------|------------------
 banana | £0.07 |    14/09/2018
--------|-------|------------------
 orange | £0.05 |    06/08/2016

I want to create a table like this. It should obviously continue if there are more elements in each list but I don't know how I would create it.

I could do

print(""" Product | Price | Date of Purchase   # etc...
          --------|-------|------------------
              %s  |   %s  |     %s 
""" % (products[0],prices[0],dates[0])) 

But I think this would be hardcoding the interface, which isn't ideal because the list has an undetermined length

Any help?

Newbie101
  • 501
  • 2
  • 6
  • 17

3 Answers3

1

Try pandas:

import pandas as pd
products = ['apple','banana','orange'] 
prices = ['£0.11','£0.07','£0.05']
dates = ['02/04/2017','14/09/2018','06/08/2016']

df = pd.DataFrame({"Product": products, "Price": prices, "Date of Purchase": dates})

print(df)

Output:

  Product  Price Date of Purchase
0   apple  £0.11       02/04/2017
1  banana  £0.07       14/09/2018
2  orange  £0.05       06/08/2016
Ernest S Kirubakaran
  • 1,524
  • 12
  • 16
1
import beautifultable
from beautifultable import BeautifulTable
table = BeautifulTable()
# made up data
products = ['apple','banana','orange'] 
prices = ['£0.11','£0.07','£0.05']
dates = ['02/04/2017','14/09/2018','06/08/2016']

table.column_headers=['Product' ,'Price','Date of Purchase']
for i in zip(products,prices,dates):
    table.append_row(list(i))
print(table)

output is :

+---------+-------+------------------+
| Product | Price | Date of Purchase |
+---------+-------+------------------+
|  apple  | £0.11 |    02/04/2017    |
+---------+-------+------------------+
| banana  | £0.07 |    14/09/2018    |
+---------+-------+------------------+
| orange  | £0.05 |    06/08/2016    |
+---------+-------+------------------+
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
1

If you want a version that doesn't utilize a library, here's a fairly simple function that makes use of some list comprehensions

def print_table(headers, *columns):
    # Ignore any columns of data that don't have a header
    columns = columns[:len(headers)]

    # Start with a space to set the header off from the left edge, then join the header strings with " | "
    print(" " + " | ".join(headers))
    # Draw the header separator with column dividers based on header length
    print("|".join(['-' * (len(header) + 2) for header in headers]))

    # Iterate over all lists passed in, and combine them together in a tuple by row
    for row in zip(*columns):
        # Center the contents within the space available in the column based on the header width
        print("|".join([
            col.center((len(headers[idx]) + 2), ' ')
            for idx, col in enumerate(row)
        ]))

This doesn't handle cell values that are longer than the column header length + 2. But that would be easy to implement with a truncation of the cell contents (an example of string truncation can be seen here).

RPiAwesomeness
  • 5,009
  • 10
  • 34
  • 52