I was trying to print a simple 2D matrix in Python 3, implementing it with nested lists. So I need something like this:
[ [1, 2, 3],
[4, 5, 6],
[7, 8, 8] ]
However, for some reasons the printed output seems to show only the last line.
Here is my code:
# I create a table with 4 rows and 5 columns, all filled by Nones:
table = [[None] * 5] * 4
# Then I initialize it with numbers:
num = 1
for i in range(4):
for j in range(5):
table[i][j] = num
num = num + 1
If now I try to print the table:
for i in range(4):
for j in range(5):
print(table[i][j], end="\t")
print("")
Here is the result:
16 17 18 19 20
16 17 18 19 20
16 17 18 19 20
16 17 18 19 20
However, I expected something like this:
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
What's wrong with my code?