0
0
NumPydata~10 mins

Converting to and 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 convert a NumPy array to a Python list.

NumPy
import numpy as np
arr = np.array([1, 2, 3])
py_list = arr.[1]()
Drag options to blanks, or click blank then click option'
Atolist()
Btolist
Cto_list
Dto_list()
Attempts:
3 left
💡 Hint
Common Mistakes
Using to_list() which does not exist.
Forgetting the parentheses after the method name.
2fill in blank
medium

Complete the code to convert a Python list to a NumPy array.

NumPy
import numpy as np
py_list = [4, 5, 6]
arr = np.[1](py_list)
Drag options to blanks, or click blank then click option'
Afromlist
Basarray
Carray
Dtolist
Attempts:
3 left
💡 Hint
Common Mistakes
Using tolist() which converts arrays to lists.
Using fromlist which is not a NumPy function.
3fill in blank
hard

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

NumPy
import numpy as np
nested_list = [[1, 2], [3, 4]]
arr = np.[1](nested_list)
Drag options to blanks, or click blank then click option'
Atolist
Barray
Casarray
Dfromlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using tolist() which converts arrays to lists.
Using fromlist which is not a valid NumPy function.
4fill in blank
hard

Fill both blanks to create a list of lengths of strings from a list using list comprehension.

NumPy
words = ['apple', 'banana', 'cherry']
lengths = [[1] for [2] in words]
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Cw
Dlen(w)
Attempts:
3 left
💡 Hint
Common Mistakes
Using word as variable but len(word) as expression without matching names.
Using len(word) but loop variable is w (mismatch).
5fill in blank
hard

Fill all three blanks to create a dictionary of word lengths for words longer than 5 characters.

NumPy
words = ['apple', 'banana', 'cherry', 'date']
length_dict = { [1]: [2] for [3] in words if len([3]) > 5 }
Drag options to blanks, or click blank then click option'
Aword
Blen(word)
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not matching the loop variable with the condition.