Closed. This question needs
This is the main reason for trying to avoid loops when using Python for ML where you have NumPy or similar libraries, as the algebra operations are quite expensive and it is necessary to optimize it as much as possible. There are many sources that compare and analyze in more detail the usage of Python list operations and NumPy operations, such as this article or this blog.