hs4545
hs4545

Reputation: 13

How do I remove the last row in the printed list?

The question asked of me is to do the following

Print the 2-dimensional list mult_table by row and column. Hint: Use nested loops. Sample output for the given program:

1 | 2 | 3
2 | 4 | 6
3 | 6 | 9

So far I have this:

mult_table = [
    [1, 2, 3],
    [2, 4, 6],
    [3, 6, 9]
]

for row in mult_table:
    for cell in row:
        print(cell, end=' | ')
    print()

The output this gives me is:

1 | 2 | 3 | 
2 | 4 | 6 | 
3 | 6 | 9 | 

I need to know how I can remove the last column of | that is being printed. Thank you for your help in advance.

Upvotes: 1

Views: 381

Answers (1)

blhsing
blhsing

Reputation: 106465

You can use the str.join method instead of always printing a pipe as an ending character:

for row in mult_table:
    print(' | '.join(map(str, row)))

Or you can use the sep parameter:

for row in mult_table:
    print(*row, sep=' | ')

Upvotes: 3

Related Questions