Python:NumPy .view()

Anonymous contributor's avatar
Anonymous contributor
Published Nov 14, 2025
Contribute to Docs

The .view() method in NumPy returns a new array object that views the same data as the original array. Both arrays share the same underlying memory block, meaning that any modification to the data in one will directly affect the other. Only the array’s metadata (such as data type or shape) may differ.

Creating a view is much faster and more memory-efficient than creating a copy, especially when working with large arrays.

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

ndarray.view([dtype][, type])

Parameters:

  • dtype (data-type): The desired data type for the new array view. Changing the dtype changes how the data bytes are interpreted, not the data itself.
  • type (type): The desired type for the resulting object (e.g., np.matrix).

Return value:

Returns a new ndarray object that shares the data of the original array.

Example

In this example, modifying the data in the view (view_array) also changes the original array (original_array) because both share the same memory:

import numpy as np
# Create the original array
original_array = np.array([1, 2, 3, 4, 5])
# Create a view of the original array
view_array = original_array.view()
print(f"Original Array before modification: {original_array}")
print(f"View Array before modification: {view_array}")
# Modify a single element in the view
view_array[0] = 99
print(f"\nOriginal Array after modifying the view: {original_array}")

The output of this code is:

Original Array before modification: [1 2 3 4 5]
View Array before modification: [1 2 3 4 5]
Original Array after modifying the view: [99 2 3 4 5]

Codebyte Example

In this example, changing the original array also updates its view, since both reference the same data in memory:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python:NumPy on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours