🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.array()

AI & DATA SCIENCE // np-array

np.array() creates a NumPy ndarray from a Python list, tuple, or other array-like object, copying the data into a single contiguous block of memory.

Syntax

np.array(object, dtype=None, copy=True)

Deep Dive Course

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.

editor.html
import numpy as np

arr = np.array([1, 2, 3])
print(arr)
print(arr.dtype)
localhost:3000

2Practical Example

Here is a real-world application of np.array() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
print(matrix)
print(matrix.shape)
localhost:3000

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.

editor.html
import numpy as np

arr = np.array([1, 2, 3])
print(arr)
print(arr.dtype)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([1, 2, 3])
print(arr)
print(arr.dtype)
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 2], [3, 4]], dtype=np.float32)
print(matrix)
print(matrix.shape)

Best Practices

  • Specify dtype explicitly (e.g. dtype=np.float32) when memory usage or precision matters, rather than relying on automatic type inference
  • Convert a Python list to an array once, outside any hot loop, rather than rebuilding arrays repeatedly inside one
  • Use np.asarray() instead of np.array() when you want to avoid an unnecessary copy of data that might already be an ndarray

Interview Question

What's the difference between np.array() and np.asarray(), and when would you prefer one over the other?

Hint: Think about what happens when the input is already an ndarray of a compatible dtype.

np.array() always copies its input data by default, even if that input is already an ndarray. np.asarray() skips the copy whenever the input is already an ndarray with a compatible dtype, returning the same underlying array instead. Prefer np.asarray() inside functions that accept array-like input but don't intend to modify it, since it avoids an unnecessary copy when the caller already passed a proper array, while still converting plain lists or other array-like inputs as needed.

Exercises

MediumPractice using np.array() in a real scenario.
View Solution
import numpy as np

arr = np.array([1, 2, 3])
print(arr)
print(arr.dtype)

Frequently Asked Questions

What's the difference between np.array() and np.asarray(), and when would you prefer one over the other?

np.array() always copies its input data by default, even if that input is already an ndarray. np.asarray() skips the copy whenever the input is already an ndarray with a compatible dtype, returning the same underlying array instead. Prefer np.asarray() inside functions that accept array-like input but don't intend to modify it, since it avoids an unnecessary copy when the caller already passed a proper array, while still converting plain lists or other array-like inputs as needed.

Related Functions

np-asarraynp-zerosndarray-dtype