Q:

How to convert a list having multiple elements with different length into a table format

I am trying to convert a list having elements in "key=value" format into a table to display to user. For this I am replacing = sign with tab and adding | at the start and at the end. This is close to what I need, but when it comes to variable length of elements, my solution breaks the formatting. something similar to column -t -s= bash comamnd is required.
import re

my_list_1=["abc=123","def=456","ghi=789","jkl=012"]
my_list_2=["abcalpha=123","def=456","ghicharlie=789","jkl=012"]

#close desired printed

for item in my_list_1:
    item_seprated=re.sub("=","\t",item)
    item_boundary=re.sub("^|$","|",item_seprated)
    print(item_boundary)

print("\n")

#undesired printed
for item in my_list_2:
    item_seprated=re.sub("=","\t",item)
    item_boundary=re.sub("^|$","|",item_seprated)
    print(item_boundary)

desired output for my_list_2:
--------------------
|key       |value  |
--------------------
|abcalpha  |    123|
|def       |    469|
|ghicharlie|    789|
|jkl       |    012|
--------------------  

A:

Also without any dependencies, but more universal function (can handle any amount of columns):
def print_table(headers, *columns):
    if len(headers) != len(columns):
        raise ValueError()
    max_lengths = [max(max(len(item) for item in column), len(header)) for header, column in zip(headers, columns)]

    def print_row(cells):
        nonlocal max_lengths
        print("|" + "|".join(cell + " " * (max_len - len(cell)) for cell, max_len in zip(cells, max_lengths)) + "|")

    line_divider = "-" * (sum(max_lengths) + len(columns) + 1)
    print(line_divider)
    print_row(headers)
    print(line_divider)
    for row in zip(*columns):
        print_row(row)
    print(line_divider)

my_list_2=["abcalpha=123", "def=456", "ghicharlie=789", "jkl=012"]
keys, values = map(list, zip(*(s.split("=") for s in my_list_2)))

print_table(("key", "value"), keys, values)
print_table(("key", "value", "source"), keys, values, my_list_2)

