17

I have data that is collected in a loop and stored under separate lists that hold only the same datatypes (e.g. only strings, only floats) as shown below:

names = ['bar', 'chocolate', 'chips']
weights = [0.05, 0.1, 0.25]
costs = [2.0, 5.0, 3.0]
unit_costs = [40.0, 50.0, 12.0]

I have treated these lists as "columns" of a table and wish to print them out as a formatted table that should look something like this:

Names     | Weights | Costs | Unit_Costs  
----------|---------|-------|------------
bar       | 0.05    | 2.0   | 40.0
chocolate | 0.1     | 5.0   | 50.0
chips     | 0.25    | 3.0   | 12.0

I only know how to print out data from lists horizontally across table rows, I have looked online (and on this site) for some help regarding this issue, however I only managed to find help for getting it to work in python 2.7 and not 3.5.1 which is what I am using.
my question is:
how do I get entries from the above 4 lists to print out into a table as shown above.

Each item index from the lists above is associated (i.e. entry[0] from the 4 lists is associated with the same item; bar, 0.05, 2.0, 40.0).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jake Cannon
  • 171
  • 1
  • 1
  • 6

3 Answers3

15

Here is a small implementation that does what you want in basic python (no special modules).


names = ['bar', 'chocolate', 'chips']
weights = [0.05, 0.1, 0.25]
costs = [2.0, 5.0, 3.0]
unit_costs = [40.0, 50.0, 12.0]


titles = ['names', 'weights', 'costs', 'unit_costs']
data = [titles] + list(zip(names, weights, costs, unit_costs))

for i, d in enumerate(data):
    line = '|'.join(str(x).ljust(12) for x in d)
    print(line)
    if i == 0:
        print('-' * len(line))

Output:


names       |weights     |costs       |unit_costs  
---------------------------------------------------
bar         |0.05        |2.0         |40.0        
chocolate   |0.1         |5.0         |50.0        
chips       |0.25        |3.0         |12.0        
Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • Thanks for this, This method appears to work very effectively and is formatted excellently. – Jake Cannon Aug 19 '16 at 07:15
  • Thanks for sharing this. Nice thing is you don't need to import a new library, downside is if the length of value is variable you will need to adjust the value passed into ljust to account for that. – Jtello Apr 27 '17 at 13:11
  • How do I make ljust to match the length of the strings? – smokingpenguin May 01 '19 at 03:32
  • Assuming you know in advance that `names` contains the longest output, add the following line: `longest_string = max(map(len, names))` and modify `line = '|'.join(str(x).ljust(longest_string + 4) for x in d)`. You can play with it and format it as suited. – François Leblanc Nov 15 '19 at 16:45
10

Some interesting table draw with texttable.

import texttable as tt
tab = tt.Texttable()
headings = ['Names','Weights','Costs','Unit_Costs']
tab.header(headings)
names = ['bar', 'chocolate', 'chips']
weights = [0.05, 0.1, 0.25]
costs = [2.0, 5.0, 3.0]
unit_costs = [40.0, 50.0, 12.0]

for row in zip(names,weights,costs,unit_costs):
    tab.add_row(row)

s = tab.draw()
print (s)

Result

+-----------+---------+-------+------------+
|   Names   | Weights | Costs | Unit_Costs |
+===========+=========+=======+============+
| bar       | 0.050   | 2     | 40         |
+-----------+---------+-------+------------+
| chocolate | 0.100   | 5     | 50         |
+-----------+---------+-------+------------+
| chips     | 0.250   | 3     | 12         |
+-----------+---------+-------+------------+

You can install texttable with using this command pip install texttable.

Rahul K P
  • 15,740
  • 4
  • 35
  • 52
-1

After visiting docs.python.org/3/library/functions.html#zip (link provided by cdarke)

I managed to find the solution I needed:

using the zip method I created a new summary list of the associated data:

# sort into rows of associated data and convert to list
rows = zip(names, weights, costs, unit_costs)
summary = list(rows)

Once I had the new summary list, I proceeded to sort and print out the table to the user (however, I will deal with the formatting later):

# Sort Alphabetically and print
summary.sort()
print()
print("*** Results shown below (alphabetically) ***")
print("Name\t\tWeight\tCost\tUnit Cost")
for item in summary:
    print("")
    for data in item:
        print(data, "\t", end='')

output is as follows:

*** Results shown below (alphabetically) ***
Name        Weight  Cost    Unit Cost

bar     0.05    2.0     40.0    
chips   0.25    3.0     12.0    
chocolate   0.1     5.0     50.0    

Thanks to cdarke for the help :)

Jake Cannon
  • 171
  • 1
  • 1
  • 6