0
0
NumPydata~20 mins

Converting to and from Python lists in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
List Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1, [2, 3], 4]
B[1, 2, 3, 4]
C[[1, 2], [3, 4]]
D[[1, 2, 3, 4]]
Attempts:
2 left
💡 Hint
Think about how a 2D numpy array converts to nested lists.
data_output
intermediate
2: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)
A(3, 2)
B(2, 3)
C(6,)
D(2,)
Attempts:
2 left
💡 Hint
Check how many rows and columns the list has.
🔧 Debug
advanced
2: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)
ASyntaxError: invalid syntax
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: could not convert string to float: 'two'
DNo error, output: array(['1', 'two', '3'], dtype='<U21')
Attempts:
2 left
💡 Hint
Numpy tries to find a common data type for all elements.
🚀 Application
advanced
2: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]]
Anp.array(nested_list).flatten()
Bnp.array(nested_list).tolist()
Cnp.array(nested_list).reshape(2, 3)
Dnp.array(nested_list).ravel().tolist()
Attempts:
2 left
💡 Hint
Flattening means making all elements in one dimension.
🧠 Conceptual
expert
3: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]])
Atolist() converts the entire array into nested Python lists; list() converts only the top-level array into a list of numpy arrays
Btolist() and list() produce identical outputs
Ctolist() flattens the array; list() preserves the shape
Dtolist() returns a numpy array; list() returns a Python list
Attempts:
2 left
💡 Hint
Think about how each method handles nested structures.