Bird
Raised Fist0
NumPydata~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand np.array() purpose

    The function np.array() is designed to take a Python list and create a NumPy array from it.
  2. 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.
  3. Final Answer:

    It converts the Python list into a NumPy array. -> Option C
  4. 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

  1. Step 1: Recall the method to convert NumPy array to list

    The correct method is tolist() called on the array object.
  2. 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.
  3. Final Answer:

    arr.tolist() -> Option A
  4. Quick Check:

    array.tolist() converts to list [OK]
Hint: Use arr.tolist() to get a Python list from NumPy array [OK]
Common Mistakes:
  • Using arr.to_list() instead of arr.tolist()
  • Trying to call tolist() as a standalone function
  • Incorrect method chaining or syntax errors
3. What is the output of this code?
import numpy as np
lst = [1, 2, 3]
arr = np.array(lst)
print(arr.tolist())
medium
A. [[1], [2], [3]]
B. array([1, 2, 3])
C. TypeError
D. [1, 2, 3]

Solution

  1. Step 1: Convert list to NumPy array

    The list [1, 2, 3] is converted to a NumPy array arr with values [1 2 3].
  2. Step 2: Convert NumPy array back to list

    Calling arr.tolist() converts the array back to a Python list [1, 2, 3].
  3. Final Answer:

    [1, 2, 3] -> Option D
  4. Quick Check:

    arr.tolist() = original list [OK]
Hint: arr.tolist() returns the original list values [OK]
Common Mistakes:
  • Expecting array output instead of list
  • Confusing print output with array representation
  • Thinking it returns nested lists
4. Identify the error in this code snippet:
import numpy as np
lst = [1, 2, 3]
arr = np.array(lst)
new_list = arr.to_list()
medium
A. arr.to_list() is not a valid method
B. np.array() cannot convert lists
C. lst should be a NumPy array already
D. Missing import statement

Solution

  1. Step 1: Check method names on NumPy arrays

    The method to convert a NumPy array to a list is tolist(), not to_list().
  2. Step 2: Identify the error in the code

    Using arr.to_list() causes an AttributeError because the method does not exist.
  3. Final Answer:

    arr.to_list() is not a valid method -> Option A
  4. Quick Check:

    Use arr.tolist(), not arr.to_list() [OK]
Hint: Use arr.tolist(), not arr.to_list() [OK]
Common Mistakes:
  • Typing to_list() instead of tolist()
  • Assuming np.array() fails on lists
  • Forgetting to import numpy
5. You have a nested Python list nested = [[1, 2], [3, 4]]. Which code correctly converts it to a NumPy array and then back to a nested Python list?
hard
A.
arr = np.array(nested)
new_list = list(arr)
B.
arr = np.array(nested)
new_list = arr.tolist()
C.
arr = list(nested)
new_list = arr.tolist()
D.
arr = np.array(nested)
new_list = arr.to_list()

Solution

  1. Step 1: Convert nested list to NumPy array

    Using np.array(nested) converts the nested list into a 2D NumPy array.
  2. Step 2: Convert NumPy array back to nested list

    Calling arr.tolist() converts the 2D array back to the original nested Python list structure.
  3. Final Answer:

    arr = np.array(nested) new_list = arr.tolist() -> Option B
  4. Quick Check:

    np.array() + tolist() preserves nested lists [OK]
Hint: Use np.array() then tolist() for nested list conversion [OK]
Common Mistakes:
  • Using list(arr) which flattens the array
  • Calling arr.to_list() which is invalid
  • Trying to convert list before np.array()
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard