fir3net
PPS-Firenetbanner-780.5x190-30-03-17

Python - Auto Width Function

Below is a python function that I created. The purpose of this function is to print correctly aligned columns using variable length items. The result is that you can pass the function a list (or list of lists), the same list is returned with each item padded to the maximum item length within the relating column.

The function works by going through all the items within the list and building a list of the length of the largest item within each of the columns. This list is then used to pad each of the items within the list to the correct size.

In addition to the list (that you need to pass this function), you also need to pass it a delimiter.

def autowidth(list,delim):
    colwidth = []
    templist = []
    ### ITERATE THROUGH EACH ITEM OF FIRST LIST ###
    for y in range(len(list[0])):
        templist.append([])
        ### ITERATE THROUGH EACH LIST  ###
        for x in range(len(list)):
            ### CREATE A LIST FOR EACH COLUMN ###
            templist[y].append(list[x][y])
    ### CREATE A LIST OF MAX COLUMN WIDTHS ###
    for y in range(len(templist)):
        max = 0
        for a in templist[y][:]:
             if len(a) > max:
            max = len(a)
        colwidth.append(max)
    ### PAD EACH LIST ITEM TO THE MAX LENGTH ###
    for y in list[:]:
        for x in range(len(colwidth)):
            y[x] = y[x].ljust(colwidth[x]) + delim
    return list

Example

>>> list = ["apples", "bananas", "pears", "orange"],["123", "12453", "125454", "21332"],["1", "2", "3", "222"],["987", "654   654", "654654", "42454654"],["464", "12", "121", "12"],["12", "123", "123123", "123123123123"]
>>>
>>> def autowidth(list,delim):
...     colwidth = []
...     templist = []
...     ### ITERATE THROUGH EACH ITEM OF FIRST LIST ###
...     for y in range(len(list[0])):
...         templist.append([])
...          ### ITERATE THROUGH EACH LIST  ###
...         for x in range(len(list)):
...             ### CREATE A LIST FOR EACH COLUMN ###
...             templist[y].append(list[x][y])
...     ### CREATE A LIST OF MAX COLUMN WIDTHS ###
...     for y in range(len(templist)):
...         max = 0
...         for a in templist[y][:]:
...                  if len(a) > max:
...                     max = len(a)
...         colwidth.append(max)
...     ### PAD EACH LIST ITEM TO THE MAX LENGTH ###
...     for y in list[:]:
...             for x in range(len(colwidth)):
...                     y[x] = y[x].ljust(colwidth[x]) + delim
...     return list
...
>>> autowidth(list,"  ")
(['apples  ', 'bananas  ', 'pears   ', 'orange        '], ['123     ', '12453    ', '125454  ', '21332         '], ['1       ', '2        ', '3       ', '222           '], ['987     ', '654654   ', '654654  ', '42454654      '], ['464     ', '12       ', '121     ', '12            '], ['12      ', '123      ', '123123  ', '123123123123  '])
>>> list = autowidth(list,"  ")
>>> for z in range(len(list)):
...     print "".join(list[z])
...
apples    bananas    pears     orange
123       12453      125454    21332
1         2          3         222
987       654654     654654    42454654
464       12         121       12
12        123        123123    123123123123
>>>

About the Author

RDonato

R Donato

Rick Donato is the Founder and Chief Editor of Fir3net.com. He currently works as a Principal Network Security Engineer and has a keen interest in automation and the cloud.

You can find Rick on Twitter @f3lix001