Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to create a zero matrix of size m*n using lists but I'm unable to print the output in desired shape

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]]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading