0

I have this matrix:

matrix = [[3,2,3],
         [-6,7-9],
         [-6,5,-12]]

How can I create an array of the same size but with zeros using Python in the most efficient and short way without using numpy?

Sergio
  • 63
  • 5

1 Answers1

1

An easy one-liner:

zeros = [[0]*len(matrix[0]) for _ in range(len(matrix))]
ThisIsAQuestion
  • 1,887
  • 14
  • 20
  • I don't think this will work. The resulting list will suffer from this problem: https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly – Jussi Nurminen Dec 04 '20 at 20:24
  • @JussiNurminen No it won't. That's the exact reason I used list comprehension instead of just doing `[[0]*len(matrix[0])]*len(matrix)`. That would cause the issue you linked. – ThisIsAQuestion Dec 04 '20 at 20:26
  • Ah, I see. Sorry about that. – Jussi Nurminen Dec 04 '20 at 20:34