Bird
Raised Fist0
NumPydata~5 mins

Converting to and from Python lists in NumPy

Choose your learning style10 modes available
Introduction

We convert between NumPy arrays and Python lists to use the best features of both. Lists are easy to use and flexible, while arrays are fast for math.

When you want to use Python functions that only accept lists.
When you want to prepare data for visualization libraries that need lists.
When you want to convert a list of data into a NumPy array for fast calculations.
When you want to save or share data in a simple Python list format.
When you want to switch between flexible lists and fast arrays during analysis.
Syntax
NumPy
import numpy as np

# Convert Python list to NumPy array
python_list = [1, 2, 3]
numpy_array = np.array(python_list)

# Convert NumPy array to Python list
converted_list = numpy_array.tolist()

Use np.array() to convert a Python list to a NumPy array.

Use array.tolist() to convert a NumPy array back to a Python list.

Examples
This shows converting an empty list to an array and an array with one element back to a list.
NumPy
import numpy as np

# Empty list to array
empty_list = []
empty_array = np.array(empty_list)
print(empty_array)

# Array to list when array has one element
single_element_array = np.array([42])
single_element_list = single_element_array.tolist()
print(single_element_list)
This example converts a nested list (2D) to a NumPy 2D array and back to a nested list.
NumPy
import numpy as np

# List with nested lists to 2D array
nested_list = [[1, 2], [3, 4]]
array_2d = np.array(nested_list)
print(array_2d)

# Convert 2D array back to nested list
list_2d = array_2d.tolist()
print(list_2d)
Shows that NumPy arrays convert mixed types to a common type (usually strings) and converting back to list keeps that type.
NumPy
import numpy as np

# List with mixed types
mixed_list = [1, 2.5, '3']
mixed_array = np.array(mixed_list)
print(mixed_array)

# Convert back to list
back_to_list = mixed_array.tolist()
print(back_to_list)
Sample Program

This program shows how to convert a Python list to a NumPy array, do a math operation, and convert back to a list.

NumPy
import numpy as np

# Create a Python list
original_list = [10, 20, 30, 40]
print('Original Python list:', original_list)

# Convert list to NumPy array
array_from_list = np.array(original_list)
print('Converted to NumPy array:', array_from_list)

# Perform a simple operation on the array
array_from_list = array_from_list * 2
print('Array after multiplication by 2:', array_from_list)

# Convert the array back to a Python list
list_from_array = array_from_list.tolist()
print('Converted back to Python list:', list_from_array)
OutputSuccess
Important Notes

Converting a list to a NumPy array is fast and allows vectorized math operations.

Converting back to a list is useful when you need Python-native data structures.

Be careful with mixed data types in lists; NumPy will convert them to a common type, often strings.

Time complexity for conversion is O(n), where n is the number of elements.

Space complexity is O(n) because a new object is created during conversion.

Use arrays when doing math, lists when you need flexibility or compatibility with Python functions.

Summary

Use np.array() to convert Python lists to NumPy arrays for fast math.

Use array.tolist() to convert NumPy arrays back to Python lists for flexibility.

Conversions are simple but watch out for data types and empty or nested lists.

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