I am currently working on a project and I need help on converting a list to a table. I have figured it out one way but there has to be a shorter and a cleaner way of doing it an
Try this:
def displaySalesReport(sales_persons_list, sales_amounts_list):
Total_Sales = sum(sales_amounts_list)
print("%-15s %-15s" %("Salespeople","Sales Amount"))
for s_person, s_amount in zip(sales_persons_list, sales_amounts_list):
print("%-15s %-15s" % (s_person, s_amount))
print("%-15s %-15s" %("Totals", Total_Sales))
This is a simplified version of your code, you could also use Python Pandas to create a table, Pandas is the best for data in tabular form but it provides much more than you need:
import pandas as pd
data = {'Salespeople': sales_persons_list, 'Sales Amount': sales_amounts_list}
df = pd.DataFrame.from_dict(data)
df.append({'Sales Amount': Total}, index="Total")
print(df)
My code is pseudo you have to modify it to fit your need.