Home > Blockchain >  Trying to create a zero matrix of size m*n using lists but I'm unable to print the output in de
Trying to create a zero matrix of size m*n using lists but I'm unable to print the output in de

Time:02-05

M=[]
for j in range(3):
   l=[]
   for k in range(3):
       l.append(0)
   M.append(l)
print(M)

OUTPUT:[[0,0,0],[0,0,0],[0,0,0]]

But I want the output to be printed like this:
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]

CodePudding user response:

to print the output in the shape that you want you can do

for item in M:
    if item is M[-1]:
        print(f"{item}]")
    elif item is M[0]:
        print(f"[{item},")
    else:
        print(f"{item},")

instead of just printing M

  •  Tags:  
  • Related