Copies vs Views

Python · NumPy Library

Theory

A view is a new array object that looks at the same memory as the original; a copy has its own data. Basic slicing returns a view, so part = prices[1:3] followed by part[0] = 0 also changes prices. Reshape, ravel and transpose usually return views too. Boolean masks and fancy indexing (prices[[0, 2]]) always return copies, and so does arr.copy().

Views make slicing big arrays cheap because nothing is duplicated, but they surprise beginners who expect a slice to be independent. To be safe, call .copy() when you plan to modify a piece and keep the original unchanged. np.shares_memory(a, b) tells you whether two arrays overlap, and arr.base is the original array for a view or None for an array that owns its data.

Why we use it

Silent changes to the original data are one of the hardest bugs to spot. Knowing which operations share memory lets you avoid accidental edits and also avoid unnecessary copies of large arrays.

When to use it

Use views (plain slices) when you only read data or deliberately want to edit the original block. Call .copy() before modifying a slice you want to keep separate, for example when cleaning a subset while keeping the raw data.

How it works

  1. Take part of an array
  2. Was it a basic slice (start:stop)? Yes: It is a view: shares memory. No: Mask or fancy index: it is a copy.
  3. Will you modify the piece? Yes: Call .copy() if the original must stay unchanged. No: A view is fine and saves memory.
  4. Edit safely

Syntax

view = arr[1:3]
copy = arr[1:3].copy()
np.shares_memory(a, b)
view.base is arr

Example

prices = np.array([100, 200, 300])
part = prices[0:2]
part[0] = 0
print(prices)   # [  0 200 300]

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic