Python:NumPy .copy()
Anonymous contributor
Published Oct 31, 2025
Contribute to Docs
The .copy() method in NumPy creates a new, independent copy of an array (ndarray). Unlike simple assignment, which creates a view that shares the same underlying data, it ensures that changes to the new array do not affect the original, and vice versa. This is useful when you need to modify an array while preserving the original data.
Syntax
ndarray.copy(order='C')
Parameters:
order(optional,str): Controls the memory layout of the copy.'C'(default): C-style (row-major) order.'F': Fortran-style (column-major) order.'A': Preserves the array’s order — Fortran if the original is Fortran-contiguous, otherwise C.'K': Keeps the order as closely as possible to the original.
Return value:
Returns a new ndarray object that is an independent copy of the original array.
Example: Basic Usage of .copy()
The following example demonstrates the difference between assignment (which shares data) and using .copy() (which creates an independent copy):
import numpy as np# Original arrayoriginal = np.array([10, 20, 30])print(f"Original array: {original}")# Assignment creates a reference (shares data)view = originalview[0] = 111print(f"Original after modifying view: {original}")# Reset original for clarityoriginal = np.array([10, 20, 30])# copy() creates an independent copycopied_array = original.copy()copied_array[0] = 999print(f"Original after modifying copy: {original}")print(f"Copied array: {copied_array}")
Output of the above example:
Original array: [10 20 30]Original after modifying view: [111 20 30]Original after modifying copy: [10 20 30]Copied array: [999 20 30]
Codebyte Example: Copying a 2D Array Using .copy()
This example shows how .copy() works with a 2D array. It modifies the copy while leaving the original unchanged:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 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