📖 Scenario: Imagine you work in a small bakery. You have a list of daily sales numbers. You want to use NumPy to do some quick calculations, but first, you need to convert your Python list into a NumPy array. Later, you want to convert the NumPy array back to a Python list to share with your team.
🎯 Goal: Learn how to convert a Python list to a NumPy array and then convert it back to a Python list.
📋 What You'll Learn
Create a Python list with exact sales numbers
Import the NumPy library
Convert the Python list to a NumPy array
Convert the NumPy array back to a Python list
Print the final Python list
💡 Why This Matters
🌍 Real World
Data scientists often receive data in Python lists but use NumPy arrays for fast calculations. Knowing how to switch between these formats helps in data cleaning and sharing results.
💼 Career
This skill is essential for data analysts and scientists who work with numerical data and need to prepare it for analysis or visualization.
Progress0 / 4 steps
1
Create the sales list
Create a Python list called sales with these exact numbers: 15, 22, 13, 27, 19.
NumPy
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Import NumPy and convert list to array
Import the NumPy library as np. Then create a NumPy array called sales_array by converting the sales list using np.array().
NumPy
Hint
Use import numpy as np to import NumPy. Use np.array() to convert the list.
3
Convert NumPy array back to Python list
Create a new variable called sales_list_again by converting the sales_array NumPy array back to a Python list using the tolist() method.
NumPy
Hint
Use the tolist() method on the NumPy array to get a Python list.
4
Print the final Python list
Print the variable sales_list_again to display the Python list converted back from the NumPy array.
NumPy
Hint
Use print() to show the list on the screen.
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]