Challenge - 5 Problems
List Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of converting numpy array to list
What is the output of this code snippet converting a numpy array to a Python list?
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) result = arr.tolist() print(result)
Attempts:
2 left
💡 Hint
Think about how a 2D numpy array converts to nested lists.
✗ Incorrect
The tolist() method converts a numpy array into a nested Python list preserving the shape. Here, a 2x2 array becomes a list of two lists, each with two elements.
❓ data_output
intermediate2:00remaining
Result of creating numpy array from list
What is the shape of the numpy array created from this list?
NumPy
import numpy as np lst = [[1, 2, 3], [4, 5, 6]] arr = np.array(lst) print(arr.shape)
Attempts:
2 left
💡 Hint
Check how many rows and columns the list has.
✗ Incorrect
The list has 2 sublists each with 3 elements, so the numpy array shape is (2, 3).
🔧 Debug
advanced2:00remaining
Identify the error when converting list to numpy array
What error does this code raise when converting a list with mixed types to a numpy array?
NumPy
import numpy as np lst = [1, 'two', 3] arr = np.array(lst) print(arr)
Attempts:
2 left
💡 Hint
Numpy tries to find a common data type for all elements.
✗ Incorrect
Numpy converts all elements to strings to create a uniform array of strings, so no error occurs.
🚀 Application
advanced2:00remaining
Convert nested list to 1D numpy array
Which option correctly converts a nested list into a flat 1D numpy array?
NumPy
import numpy as np nested_list = [[1, 2], [3, 4], [5, 6]]
Attempts:
2 left
💡 Hint
Flattening means making all elements in one dimension.
✗ Incorrect
The flatten() method returns a copy of the array collapsed into one dimension.
🧠 Conceptual
expert3:00remaining
Understanding difference between tolist() and list() on numpy arrays
What is the key difference between using tolist() and list() on a numpy array?
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]])
Attempts:
2 left
💡 Hint
Think about how each method handles nested structures.
✗ Incorrect
tolist() recursively converts all elements to Python lists, preserving nested structure. list() converts only the outer array to a list, keeping inner elements as numpy arrays.