Bird
Raised Fist0
NumPydata~20 mins

flatten() and ravel() for 1D conversion in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Flatten and Ravel Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of flatten() vs ravel() on a 2D array
What is the output of the following code snippet?
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat = arr.flatten()
rav = arr.ravel()
flat[0] = 100
rav[1] = 200
print(arr)
print(flat)
print(rav)
A
[[1 2]
 [3 4]]
[100   2   3   4]
[1 200 3 4]
B
[[100 2]
 [3 4]]
[100   2   3   4]
[1 200 3 4]
C
[[1 2]
 [3 4]]
[1 2 3 4]
[1 2 3 4]
D
[[1 200]
 [3 4]]
[100   2   3   4]
[1 200 3 4]
Attempts:
2 left
💡 Hint
Remember that flatten() returns a copy, while ravel() returns a view when possible.
❓ data_output
intermediate
1:30remaining
Shape of arrays after flatten() and ravel()
Given the code below, what are the shapes of flat and rav?
NumPy
import numpy as np
arr = np.arange(12).reshape(3,4)
flat = arr.flatten()
rav = arr.ravel()
print(flat.shape)
print(rav.shape)
A
(3,4)
(12,)
B
(12,)
(12,)
C
(12,)
(3,4)
D
(3,4)
(3,4)
Attempts:
2 left
💡 Hint
Both flatten() and ravel() convert the array to 1D.
🔧 Debug
advanced
1:30remaining
Why does modifying ravel() output change the original array?
Consider this code: import numpy as np arr = np.array([[5, 6], [7, 8]]) r = arr.ravel() r[0] = 100 print(arr) Why does arr change after modifying r?
NumPy
import numpy as np
arr = np.array([[5, 6], [7, 8]])
r = arr.ravel()
r[0] = 100
print(arr)
Aravel() returns a copy, so changes to r do not affect arr.
Barr is immutable, so changes to r raise an error.
Cravel() returns a view of the original array, so changes to r affect arr.
Dravel() flattens the array but does not allow modification.
Attempts:
2 left
💡 Hint
Think about whether ravel() returns a copy or a view.
🧠 Conceptual
advanced
1:30remaining
Memory usage difference between flatten() and ravel()
Which statement about memory usage is true when comparing flatten() and ravel()?
Aflatten() always creates a copy, using more memory; ravel() returns a view when possible, using less memory.
Bflatten() returns a view, so it uses less memory; ravel() always copies the data.
CBoth flatten() and ravel() always create copies, so memory usage is the same.
DBoth flatten() and ravel() always return views, so memory usage is minimal.
Attempts:
2 left
💡 Hint
Check which method returns a copy and which returns a view.
🚀 Application
expert
2:30remaining
Choosing flatten() or ravel() for safe modification
You have a large 2D numpy array and want to create a 1D version to modify without changing the original array. Which method should you use and why?
AUse flatten() because it returns a copy, so the original array stays unchanged.
BUse ravel() because it returns a view, so changes reflect in the original array.
CUse flatten() because it returns a view, so changes reflect in the original array.
DUse ravel() because it returns a copy, so the original array stays unchanged.
Attempts:
2 left
💡 Hint
Think about whether you want changes to affect the original array or not.

Practice

(1/5)
1. What is the main difference between flatten() and ravel() in NumPy?
easy
A. Both return copies but differ in speed.
B. flatten() returns a view, ravel() returns a copy.
C. Both return views but differ in shape.
D. flatten() returns a copy, ravel() returns a view if possible.

Solution

  1. Step 1: Understand flatten() behavior

    flatten() always returns a new 1D copy of the array, so changes to the result do not affect the original.
  2. Step 2: Understand ravel() behavior

    ravel() returns a 1D view if possible, meaning it shares data with the original array and changes affect the original. If a view is not possible, it returns a copy.
  3. Final Answer:

    flatten() returns a copy, ravel() returns a view if possible. -> Option D
  4. Quick Check:

    Copy vs view difference = flatten() returns a copy, ravel() returns a view if possible. [OK]
Hint: Remember: flatten copies, ravel tries to share data [OK]
Common Mistakes:
  • Thinking both always return copies
  • Confusing which returns a view
  • Assuming ravel always returns a copy
2. Which of the following is the correct syntax to convert a 2D NumPy array arr into a 1D array using ravel()?
easy
A. arr.flatten()
B. ravel(arr)
C. arr.ravel()
D. arr.ravel

Solution

  1. Step 1: Recall method usage

    ravel() is a method of a NumPy array, so it is called as arr.ravel().
  2. Step 2: Check other options

    ravel(arr) is incorrect because ravel is not a standalone function in NumPy; arr.flatten() is valid but uses flatten, not ravel; arr.ravel misses parentheses, so it returns a method object, not the result.
  3. Final Answer:

    arr.ravel() -> Option C
  4. Quick Check:

    Method call syntax = arr.ravel() [OK]
Hint: Use parentheses to call methods like ravel() [OK]
Common Mistakes:
  • Forgetting parentheses when calling methods
  • Using ravel as a standalone function
  • Confusing flatten() with ravel() syntax
3. What will be the output of the following code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
flat = arr.flatten()
flat[0] = 100
print(arr[0, 0])
medium
A. 100
B. 1
C. 0
D. Error

Solution

  1. Step 1: Understand flatten() creates a copy

    flat = arr.flatten() makes a new 1D copy of arr. Changes to flat do not affect arr.
  2. Step 2: Check the effect of modifying flat

    Changing flat[0] to 100 does not change arr[0, 0], so arr[0, 0] remains 1.
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    flatten() copy means original unchanged = 1 [OK]
Hint: flatten() copies, so original array stays same after changes [OK]
Common Mistakes:
  • Assuming flatten returns a view and changes original
  • Confusing flatten() with ravel() behavior
  • Expecting arr[0,0] to change after modifying flat
4. The following code is intended to flatten a 2D array and modify the first element, but it does not change the original array. What is the mistake?
import numpy as np
arr = np.array([[5, 6], [7, 8]])
flat = arr.flatten()
flat[0] = 999
print(arr)
medium
A. Using flatten() which returns a copy, not a view
B. Forgetting to use parentheses with flatten
C. Modifying flat instead of arr directly
D. arr is not a NumPy array

Solution

  1. Step 1: Identify flatten() behavior

    flatten() returns a new copy of the array, so changes to flat do not affect arr.
  2. Step 2: Understand why original array is unchanged

    Since flat is a copy, modifying flat[0] does not change arr. To modify original, use ravel() or modify arr directly.
  3. Final Answer:

    Using flatten() which returns a copy, not a view -> Option A
  4. Quick Check:

    flatten() returns copy = Using flatten() which returns a copy, not a view [OK]
Hint: Use ravel() to get a view if you want to modify original [OK]
Common Mistakes:
  • Assuming flatten() returns a view
  • Not using parentheses with flatten()
  • Modifying the copy instead of original array
5. You have a large 3D NumPy array data and want to flatten it into 1D for fast processing without using extra memory. Which method should you use and why?
hard
A. Use data.ravel() because it returns a view if possible, saving memory.
B. Use data.tolist() to convert to list and flatten.
C. Use data.reshape(-1) because it always returns a copy.
D. Use data.flatten() because it returns a copy and is safer.

Solution

  1. Step 1: Understand memory use of flatten() vs ravel()

    flatten() always creates a new copy, using extra memory. ravel() returns a view if possible, avoiding extra memory use.
  2. Step 2: Consider large data and performance

    For large arrays, avoiding copies saves memory and speeds up processing. ravel() is preferred for this reason.
  3. Step 3: Check other options

    reshape(-1) returns a view if possible, but not guaranteed; tolist() converts to Python list, which is slower and uses more memory.
  4. Final Answer:

    Use data.ravel() because it returns a view if possible, saving memory. -> Option A
  5. Quick Check:

    ravel() view saves memory = Use data.ravel() because it returns a view if possible, saving memory. [OK]
Hint: Use ravel() for memory-efficient flattening [OK]
Common Mistakes:
  • Choosing flatten() and wasting memory
  • Confusing reshape(-1) behavior
  • Using tolist() which is slow and memory heavy
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