Skip to content Skip to sidebar Skip to footer

How To Print Values From Lists In Tabular Format (python)?

Using python 2.7, I want to display the values in tabular format, without using pandas/prettytable. I am a beginner to python and am trying to learn. Below is the values I have in

Solution 1:

You're pretty close!

def print_results_table(data, listA):
    str_l = max(len(t) for t in listA)
    str_l += 2 # add two spaces between elements
    # print the titles
    for title in listA:
        print('{:<{length}s}'.format(title, length = str_l), end='')
    print()

    # print the values
    for row in data:
        for val in row:
            print('{:<{length}s}'.format(val, length = str_l), end='')
        print()

Output:

Alpha  Beta   gama   cat    
A      B      C      D      
E      F      G      H      
I      J      K      L      
M      N      O      P 

Post a Comment for "How To Print Values From Lists In Tabular Format (python)?"