Q:

How to format a list in Python as a table

I have two separate lists that I want to format as a table.
keys = ('banana', 'broccoli', 'beans', 'apple')
values = (5, 1, 1, 1)

In the list above, it counts how many fruits there are available. For instance, there are 5 bananas and 1 broccoli. Note that the names of the fruit have different lengths. Preferably, I would like to not have to import any modules. Is there a way that I can format these lists so that it looks like:
'Banana'   | 5
'Broccoli' | 1
'Beans'    | 1
'Apple'    | 1

I have already tried using this code:
row_format ="{:>15}" * (len(items) + 1)
print(row_format.format("", *items))
for keys, values in zip(items, keys):
    print(row_format.format(values, *row))

Thanks for the help!

A:

keys = ('banana', 'broccoli', 'beans', 'apple')
values = (5, 1, 1, 1)

max_length = len(max(keys, key=len))

for k, v in zip(keys, values):
    print("{: <{width}} | {}".format(repr(k).title(), v, width=max_length + 2))

Prints:
'Banana'   | 5
'Broccoli' | 1
'Beans'    | 1
'Apple'    | 1

