Bird
Raised Fist0
NumPydata~20 mins

Indexing returns views not copies in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
View vs Copy Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of modifying a view created by slicing a NumPy array?
Consider the following code where we create a NumPy array and slice it. What will be the content of the original array after modifying the slice?
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
slice_arr = arr[1:4]
slice_arr[0] = 99
print(arr)
A[10 99 30 40 50]
B[10 20 30 40 50]
C[10 99 99 40 50]
D[10 20 99 40 50]
Attempts:
2 left
💡 Hint
Remember that slicing a NumPy array returns a view, not a copy.
❓ Predict Output
intermediate
2:00remaining
What happens when you modify a copy created by fancy indexing in NumPy?
Look at this code where we use fancy indexing to select elements. What will be the output of the original array after modifying the selected elements?
NumPy
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
selected = arr[[1, 3]]
selected[0] = 99
print(arr)
A[1 99 3 4 5]
B[1 2 99 4 5]
C[1 2 3 4 5]
D[1 99 3 99 5]
Attempts:
2 left
💡 Hint
Fancy indexing returns a copy, not a view.
🔧 Debug
advanced
2:30remaining
Why does modifying a slice not change the original array here?
This code tries to modify a slice of a 2D NumPy array but the original array remains unchanged. What is the reason?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6]])
slice_arr = arr[[0, 2]]
slice_arr[0, 0] = 99
print(arr)
ABecause NumPy arrays are immutable.
BBecause the array is 2D, slices always return copies.
CBecause the assignment syntax is incorrect.
DBecause fancy indexing returns a copy, not a view.
Attempts:
2 left
💡 Hint
Check the type of indexing used to create the slice.
❓ data_output
advanced
2:00remaining
How many elements are changed in the original array after modifying a boolean mask view?
Given this code, how many elements in the original array are changed after modifying the masked view?
NumPy
import numpy as np
arr = np.array([5, 10, 15, 20, 25])
mask = arr > 10
view = arr[mask]
view[:] = 0
print(arr)
A0
B2
C3
D5
Attempts:
2 left
💡 Hint
Boolean indexing returns a copy, not a view.
🚀 Application
expert
3:00remaining
How to safely modify a subset of a NumPy array without affecting the original?
You want to modify a subset of a NumPy array without changing the original array. Which approach below correctly achieves this?
A
subset = arr[[1, 2, 3]]
subset[0] = 100
B
subset = arr[1:4].copy()
subset[0] = 100
C
subset = arr[1:4]
subset[0] = 100
D
subset = arr
subset[1] = 100
Attempts:
2 left
💡 Hint
Think about when views or copies are created and how to avoid changing the original.

Practice

(1/5)
1. What happens when you create a slice of a NumPy array using indexing like arr[1:4]?
easy
A. It returns a view that shares data with the original array.
B. It returns a completely independent copy of the array slice.
C. It returns a list instead of a NumPy array.
D. It raises an error because slicing is not allowed.

Solution

  1. Step 1: Understand NumPy slicing behavior

    In NumPy, slicing an array returns a view, not a copy. This means the new array shares the same data buffer as the original.
  2. Step 2: Consequence of view sharing data

    Because the slice is a view, changes to it will affect the original array, confirming it is not an independent copy.
  3. Final Answer:

    It returns a view that shares data with the original array. -> Option A
  4. Quick Check:

    Indexing slice = view sharing data [OK]
Hint: Slicing returns views, not copies, by default [OK]
Common Mistakes:
  • Thinking slicing creates a copy
  • Confusing views with copies
  • Assuming slicing returns a list
2. Which of the following is the correct way to create a copy of a NumPy array slice so that changes do NOT affect the original array?
easy
A. slice = arr[2:5]
B. slice = arr.copy[2:5]
C. slice = arr[2:5].copy()
D. slice = arr[2:5].view()

Solution

  1. Step 1: Identify method to create independent copy

    The .copy() method creates a new array with its own data, independent of the original.
  2. Step 2: Check syntax correctness

    Only arr[2:5].copy() correctly calls copy() on the slice. Other options either miss parentheses or use wrong methods.
  3. Final Answer:

    slice = arr[2:5].copy() -> Option C
  4. Quick Check:

    Use .copy() for independent array [OK]
Hint: Use .copy() after slicing to avoid shared data [OK]
Common Mistakes:
  • Forgetting parentheses on .copy()
  • Using .view() instead of .copy()
  • Assuming slicing alone makes a copy
3. Consider the code:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
slice_arr = arr[1:4]
slice_arr[0] = 99
print(arr)

What is the output?
medium
A. [10 99 30 40 50]
B. [10 20 30 40 50]
C. [10 99 99 40 50]
D. [10 20 99 40 50]

Solution

  1. Step 1: Understand slice is a view

    The slice arr[1:4] returns a view sharing data with arr. Changing slice_arr[0] changes arr[1].
  2. Step 2: Apply the change and print

    Setting slice_arr[0] = 99 updates arr[1] to 99. The rest of arr remains unchanged.
  3. Final Answer:

    [10 99 30 40 50] -> Option A
  4. Quick Check:

    View change updates original array [OK]
Hint: Changing slice changes original array if no copy [OK]
Common Mistakes:
  • Expecting original array unchanged
  • Changing wrong indices
  • Confusing slice indices with original
4. You run this code:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
slice_arr = arr[1:4]
slice_arr = slice_arr + 10
print(arr)

Why does arr remain unchanged?
medium
A. Because the print statement is incorrect.
B. Because slice_arr + 10 creates a new array, not modifying the view.
C. Because arr is immutable.
D. Because slicing always creates a copy, not a view.

Solution

  1. Step 1: Analyze the operation slice_arr + 10

    This operation creates a new array with values increased by 10. It does not modify slice_arr in place.
  2. Step 2: Understand assignment effect

    Assigning slice_arr = slice_arr + 10 makes slice_arr point to a new array. The original arr remains unchanged because no in-place modification happened.
  3. Final Answer:

    Because slice_arr + 10 creates a new array, not modifying the view. -> Option B
  4. Quick Check:

    slice + scalar creates new array, no original change [OK]
Hint: Adding to slice creates new array, original stays same [OK]
Common Mistakes:
  • Assuming slice_arr + 10 modifies original array
  • Thinking slicing always copies
  • Believing arrays are immutable
5. You have a large NumPy array data and want to modify a slice without affecting the original array. Which approach is best to ensure safety and memory efficiency?
hard
A. Use slice = data[100:200] and modify slice directly.
B. Modify data directly without slicing.
C. Use slice = data[100:200].view() and modify slice.
D. Use slice = data[100:200].copy() to modify safely without changing data.

Solution

  1. Step 1: Understand views vs copies for safety

    Modifying a view changes the original array. To avoid this, a copy is needed.
  2. Step 2: Consider memory efficiency

    Copying only the needed slice with .copy() uses less memory than copying the whole array and ensures safe modification.
  3. Step 3: Evaluate other options

    Using slice = data[100:200] and modifying slice directly modifies the original data unintentionally. Using slice = data[100:200].view() and modifying slice creates a view, which is not safe. Modifying data directly without slicing affects the entire array, not just the slice.
  4. Final Answer:

    Use slice = data[100:200].copy() to modify safely without changing data. -> Option D
  5. Quick Check:

    Copy slice for safe, efficient modification [OK]
Hint: Copy slice to avoid changing original large array [OK]
Common Mistakes:
  • Modifying views thinking they are copies
  • Copying entire array unnecessarily
  • Ignoring memory use in large arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard