0
0
NumPydata~20 mins

np.array() from Python lists in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
np.array() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
Output of np.array() with nested lists
What is the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr.shape)
A(2, 2)
B(4,)
C(1, 4)
D(2,)
Attempts:
2 left
💡 Hint
Think about how many rows and columns the nested list has.
data_output
intermediate
1:30remaining
Data type of np.array from mixed Python lists
What is the data type of the numpy array created by this code?
import numpy as np
arr = np.array([1, 2.5, 3])
print(arr.dtype)
NumPy
import numpy as np
arr = np.array([1, 2.5, 3])
print(arr.dtype)
Abool
Bint64
Cfloat64
Dobject
Attempts:
2 left
💡 Hint
Numpy converts all elements to the same type, choosing the one that can hold all values.
🔧 Debug
advanced
2:00remaining
Error when creating np.array from irregular nested lists
What error does this code raise?
import numpy as np
arr = np.array([[1, 2], [3, 4, 5]])
print(arr)
NumPy
import numpy as np
arr = np.array([[1, 2], [3, 4, 5]])
print(arr)
AValueError: setting an array element with a sequence
BSyntaxError
CTypeError: unhashable type: 'list'
DVisible array with shape (2,)
Attempts:
2 left
💡 Hint
Check how numpy handles nested lists with different lengths.
🚀 Application
advanced
1:30remaining
Creating a 3D numpy array from Python lists
Which option creates a numpy array with shape (2, 2, 2)?
Anp.array([[[1,2],[3,4]], [[5,6],[7,8]]])
Bnp.array([[1,2],[3,4],[5,6],[7,8]])
Cnp.array([1,2,3,4,5,6,7,8])
Dnp.array([[1,2,3],[4,5,6]])
Attempts:
2 left
💡 Hint
Count the number of nested brackets and elements per level.
🧠 Conceptual
expert
2:00remaining
Why does np.array() sometimes create dtype=object?
Why does numpy create an array with dtype=object when given certain Python lists?
ABecause the Python list contains only integers.
BBecause the nested lists have different lengths or types, so numpy cannot create a uniform numeric array.
CBecause numpy always defaults to dtype=object for all Python lists.
DBecause numpy does not support nested lists.
Attempts:
2 left
💡 Hint
Think about numpy's need for uniform data types and shapes.