0
0
NumPydata~10 mins

np.array() from Python lists in NumPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a NumPy array from a Python list.

NumPy
import numpy as np
my_list = [1, 2, 3]
arr = np.[1](my_list)
print(arr)
Drag options to blanks, or click blank then click option'
Aarray
Blist
Carray_list
Dtoarray
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'list' instead of 'array' causes an error because 'list' is not a NumPy function.
Trying to use 'toarray' which is not a valid NumPy function.
2fill in blank
medium

Complete the code to create a 2D NumPy array from a list of lists.

NumPy
import numpy as np
my_list = [[1, 2], [3, 4]]
arr = np.[1](my_list)
print(arr.shape)
Drag options to blanks, or click blank then click option'
Amatrix
Blist
Carray
Dtolist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'matrix' which is deprecated in NumPy.
Using 'tolist' which converts arrays back to lists.
3fill in blank
hard

Fix the error in the code to convert a list to a NumPy array.

NumPy
import numpy as np
my_list = [5, 6, 7]
arr = np.[1](my_list)
print(arr)
Drag options to blanks, or click blank then click option'
Aarray
BArray
Carr
Dtolist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Array' with uppercase 'A' causes an AttributeError.
Using 'tolist' which is a method to convert arrays back to lists.
4fill in blank
hard

Fill both blanks to create a NumPy array and check its data type.

NumPy
import numpy as np
my_list = [1, 2, 3]
arr = np.[1](my_list)
print(arr.[2])
Drag options to blanks, or click blank then click option'
Aarray
Bshape
Cdtype
Dtolist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'shape' instead of 'dtype' to check data type.
Using 'tolist' which converts arrays back to lists.
5fill in blank
hard

Fill all three blanks to create a 2D array, get its shape, and convert it back to a list.

NumPy
import numpy as np
my_list = [[1, 2], [3, 4]]
arr = np.[1](my_list)
print(arr.[2])
print(arr.[3]())
Drag options to blanks, or click blank then click option'
Aarray
Bshape
Ctolist
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtype' instead of 'shape' to get dimensions.
Forgetting parentheses when calling 'tolist()'.