Skip to content Skip to sidebar Skip to footer

How Can I Append \n At The End Of The List In List Comperhansion

I have this code: def table(h, w): table = [['.' for i in range(w)] for j in range(h)] return table which returns this [ ['.', '.', '.'], ['.', '.', '.'], ['.', '.', '.'],

Solution 1:

The (only) way to do it is actually to format the result (list type) to a string :

def table(h, w):
    table = [['.' for i in range(w)] for j in range(h)]
    return table
def format_table(table_):
    return "[\n" + ''.join(["\t" + str(line) + ",\n" for line in table_]) + "]"
print(format_table(table(3,3)))

Output :

[
    ['.', '.', '.'],
    ['.', '.', '.'],
    ['.', '.', '.'],
]

Post a Comment for "How Can I Append \n At The End Of The List In List Comperhansion"