Unlike a Python list, which stores pointers to scattered objects, np.array() packs its elements into one contiguous block of memory, all sharing a single, fixed dtype, like int64 or float32, inferred automatically from the input unless you specify one explicitly. This uniform, contiguous layout is what lets NumPy delegate arithmetic and other operations to optimized, pre-compiled C loops instead of Python's much slower interpreted loop, which is the entire performance case for using NumPy over plain lists.
1Understanding np.array()
Unlike a Python list, which stores pointers to scattered objects, np.array() packs its elements into one contiguous block of memory, all sharing a single, fixed dtype, like int64 or float32, inferred automatically from the input unless you specify one explicitly. This uniform, contiguous layout is what lets NumPy delegate arithmetic and other operations to optimized, pre-compiled C loops instead of Python's much slower interpreted loop, which is the entire performance case for using NumPy over plain lists.
By default np.array() always copies its input data — pass copy=False to avoid the copy when the input is already an array of a compatible dtype and you don't need an independent copy, though np.asarray() is the more common way to express that same intent.
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
print(arr.dtype)2Practical Example
Here is a real-world application of np.array() showing how it is used in production NumPy code.
import numpy as np
matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
print(matrix)
print(matrix.shape)3Best Practices
Follow these guidelines when working with np.array():
1. Specify dtype explicitly (e.g. dtype=np.float32) when memory usage or precision matters, rather than relying on automatic type inference
2. Convert a Python list to an array once, outside any hot loop, rather than rebuilding arrays repeatedly inside one
3. Use np.asarray() instead of np.array() when you want to avoid an unnecessary copy of data that might already be an ndarray
Tip: By default np.array() always copies its input data — pass copy=False to avoid the copy when the input is already an array of a compatible dtype and you don't need an independent copy, though np.asarray() is the more common way to express that same intent.
import numpy as np
arr = np.array([1, 2, 3])
print(arr)
print(arr.dtype)