Following is my code:
inv = {'rope': 1, 'torch': 6, 'gold coin': 42, 'dagger': 1}
x=max(inv, key=lambda x: len(x.split()))
y=len(x)
#print(y)
n = "-"
q = n * (y+5)
#print(q)
#print("")
def print_table(inventory, order=None):
    if order=="count,asc":
        for key, value in sorted(inventory.iteritems(), key=lambda (k, v): (v, k)):
            print "%s %s" % (value, key)
print_table(inv,"count,asc")
I want to have something like this:
Inventory:
 count    item name
   ----------------
    45    gold coin
    12        arrow
     6        torch
     2       dagger
     1         rope
     1         ruby
    ----------------
  Total number of items: 67
Order parameter must be a string, which works as following:
empty (by default) means the table is unordered
"count,desc" table is ordered by count (of items in the inventory) in descending order and "cound,asc" in ascending order
I wrote function that's finding longest string in each of the inner lists to know how wide should be column to fit all strings, but now I'm stuck at this point, what should I do now?
 
                        
You have the right idea to find the longest key and its length. All you need is to format it:
Output:
Update
To address different sort columns and order (ascending, descending), I would like to suggest not passing in a string, but a key function and sort direction (ascending or descending):
Note: By default, the
sortedfunction will sort adict.items()by (k, v), so theby_namefunction can beNone, or it can be a more explicitlambda (k, v): (k, v)