How to add a header and align lists

Question:

I have this data set that I need to align the columns of ID, Date, and Title. I’m not sure how to align these and how to add a header? I’m not able to use any 3rd party functions like tabulate. Could someone walk me through on how to do this? I’m a beginner and just need some guidance!

data_set = [['ID=j234hgn'], ['Date=19 October 1969n'], ['Title=court scene with cardinal richelieun'], ['ID=d45j5jkdn'], ['Date=28 December 1969n'], ['Title=THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOMn'], ['ID=s4k5jkn'], ['Date=8 December 1970n'], ['Title=crossing the atlantic on a tricyclen'], ['ID=zd7u4hn'], ['Date=19 October 1969n'], ['Title=Bicycle Repair Mann'], ['ID=f983n'], ['Date=22 December 1970n'], ['Title=Royal Episode 13 (or: The Queen Will Be Watching)n'], ['ID=j8s74n'], ['Date=15 September 1970n'], ['Title=THE SEMAPHORE VERSION OF WUTHERING HEIGHTSn'], ['ID=n4j6l3jn'], ['Date=7 December 1972n'], ['Title=Mr. Pither']]

for index,l in enumerate(data_set):
    column_name,value = l[0].split("=")
    if 'Title' == column_name:
        data_set[index]="=".join([column_name+value.title()])

print (data_set)

rows = [ data_set[i:i+3] for i in range(0,len(data_set), 3)]
print (sum(sorted(rows, key = lambda r:r[0][0]), []))

It needs to look like this:

#ID       Date              Title
#d45j5jkd 28 December 1969  The Royal Philharmonic Orchestra Goes To The Bathroom
#f983     22 December 1970  Royal Episode 13 (Or: The Queen Will Be Watching)
Asked By: learningpython

||

Answers:

You can use the Python3.x Format Specification Mini-Language to format and align the headers and the data_set.

Unfortunately the data_set is a list of sub-lists, so it starts a little messy as we need to Flatten list of lists, then printing in the for-loop uses named placeholders and string padding to print groups of 3 from the flattened list:

data_set = [['ID=j234hgn'], ['Date=19 October 1969n'], ['Title=court scene with cardinal richelieun'], 
        ['ID=d45j5jkdn'], ['Date=28 December 1969n'], ['Title=THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOMn'], 
        ['ID=s4k5jkn'], ['Date=8 December 1970n'], ['Title=crossing the atlantic on a tricyclen'], 
        ['ID=zd7u4hn'], ['Date=19 October 1969n'], ['Title=Bicycle Repair Mann'], ['ID=f983n'], 
        ['Date=22 December 1970n'], ['Title=Royal Episode 13 (or: The Queen Will Be Watching)n'], 
        ['ID=j8s74n'], ['Date=15 September 1970n'], ['Title=THE SEMAPHORE VERSION OF WUTHERING HEIGHTSn'], 
        ['ID=n4j6l3jn'], ['Date=7 December 1972n'], ['Title=Mr. Pither']]

#First, flatten the list of lists to one list of strings
flist = [item for sublist in data_set for item in sublist]

print('{h1:10}{h2:20}{h3}'.format(h1='#ID', h2='Date', h3='Title'))
for i in range(0, len(flist), 3):
    print('{id:10}{date:20}{title}'   
                       .format(   
                               id=flist[i].strip('n').split('=')[1],   
                               date=flist[i+1].strip('n').split('=')[1],   
                               title=flist[i+2].strip('n').split('=')[1])
                              )

Output:

#ID       Date                Title
j234hg    19 October 1969     court scene with cardinal richelieu
d45j5jkd  28 December 1969    THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM
s4k5jk    8 December 1970     crossing the atlantic on a tricycle
zd7u4h    19 October 1969     Bicycle Repair Man
f983      22 December 1970    Royal Episode 13 (or: The Queen Will Be Watching)
j8s74     15 September 1970   THE SEMAPHORE VERSION OF WUTHERING HEIGHTS
n4j6l3j   7 December 1972     Mr. Pither

Feel free to comment if it doesn’t exactly answer your question.
I apologize as it is very messy but it works. I’ll try to clean it up later. In the meantime, I hope it helps.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.