How do you convert a Python list to a NumPy array?
Use numpy.array() function. For example, np.array([1, 2, 3]) converts the list [1, 2, 3] to a NumPy array.
Click to reveal answer
beginner
How do you convert a NumPy array back to a Python list?
Use the tolist() method of the NumPy array. For example, arr.tolist() converts the array arr to a Python list.
Click to reveal answer
beginner
What is the difference between a Python list and a NumPy array?
A Python list can hold items of different types and is flexible but slower. A NumPy array holds items of the same type and is optimized for fast numerical operations.
Click to reveal answer
intermediate
Can you convert a nested Python list to a NumPy array? How?
Yes, you can convert nested lists (lists of lists) to multi-dimensional NumPy arrays using np.array(). For example, np.array([[1, 2], [3, 4]]) creates a 2x2 array.
Click to reveal answer
intermediate
What happens if you convert a NumPy array with mixed data types to a list?
The tolist() method converts the array elements to native Python types, preserving the structure. If the array has mixed types, the list will contain those types as Python objects.
Click to reveal answer
Which function converts a Python list to a NumPy array?
Aarray.tolist()
Bnumpy.tolist()
Cnumpy.array()
Dlist()
✗ Incorrect
The numpy.array() function converts a Python list to a NumPy array.
How do you convert a NumPy array named arr to a Python list?
Aarr.tolist()
Barr.to_list()
Clist(arr)
Dnumpy.list(arr)
✗ Incorrect
Use the tolist() method: arr.tolist().
What type of data structure does np.array() create from a list?
ANumPy array
BPython list
CDictionary
DTuple
✗ Incorrect
np.array() creates a NumPy array.
Can np.array() convert a nested list into a multi-dimensional array?
AOnly for 1D lists
BNo
COnly if the nested lists are the same length
DYes
✗ Incorrect
Yes, np.array() can convert nested lists into multi-dimensional arrays.
What does arr.tolist() return?
AA NumPy array
BA Python list
CA Python tuple
DA Python dictionary
✗ Incorrect
arr.tolist() returns a Python list.
Explain how to convert a Python list to a NumPy array and back to a list.
Think about the functions and methods used for conversion.
You got /3 concepts.
Describe the difference between Python lists and NumPy arrays in terms of data type and performance.
Consider flexibility vs speed.
You got /3 concepts.
Practice
(1/5)
1. What does the function np.array() do when given a Python list?
easy
A. It deletes the Python list.
B. It converts the Python list into a Python dictionary.
C. It converts the Python list into a NumPy array.
D. It converts the Python list into a string.
Solution
Step 1: Understand np.array() purpose
The function np.array() is designed to take a Python list and create a NumPy array from it.
Step 2: Compare options with function behavior
Only It converts the Python list into a NumPy array. correctly states that it converts a list to a NumPy array. Other options describe unrelated actions.
Final Answer:
It converts the Python list into a NumPy array. -> Option C
Quick Check:
np.array(list) = NumPy array [OK]
Hint: np.array() always makes a NumPy array from a list [OK]
Common Mistakes:
Thinking np.array() converts to dict or string
Confusing np.array() with list methods
Assuming np.array() deletes data
2. Which of the following is the correct syntax to convert a NumPy array arr back to a Python list?
easy
A. arr.tolist()
B. arr.to_list()
C. tolist(arr)
D. list(arr.to)
Solution
Step 1: Recall the method to convert NumPy array to list
The correct method is tolist() called on the array object.
Step 2: Check each option's syntax
arr.tolist() uses arr.tolist(), which is correct. arr.to_list() has a wrong method name. Options C and D are invalid syntax.
Final Answer:
arr.tolist() -> Option A
Quick Check:
array.tolist() converts to list [OK]
Hint: Use arr.tolist() to get a Python list from NumPy array [OK]