How to Convert NumPy Array to List?
I understand that learning data science can be really challenging…
…especially when you are just starting out.
But it doesn’t have to be this way.
That’s why I spent weeks creating a 46-week Data Science Roadmap with projects and study resources for getting your first data science job.
Here’s what it contains:
- Complete roadmap with study resources
2. 20+ practice problems for each week
3. A resources hub that contains:
- Free-to-read books
- YouTube channels for data scientists
- Free courses
- Top GitHub repositories
- Free APIs
- List of data science communities to join
- Project ideas
- And much more…
If that’s not enough, I’ve also added:
4. A Discord community to help our data scientist buddies get access to study resources, projects, and job referrals.
Like what you’re seeing?
Click here to access everything!
Now, let’s get back to the blog:
Alright, let’s dive into the practical side of things.
By the end of this blog, you’ll be confident in converting NumPy arrays into Python lists using simple, beginner-friendly methods.
I’ll walk you through the process step-by-step, keeping it straightforward and relatable.
a. Using the tolist()
Method
Let’s start with the easiest and most commonly used method: the tolist()
method. If you’ve ever thought "There must be a one-line solution for this!", you’re absolutely right. NumPy provides this built-in method to convert arrays into lists in the blink of an eye.
Here’s how it works:
import numpy as np
# Create a NumPy array
array = np.array([1, 2, 3, 4, 5])
# Convert NumPy array to list
list_result = array.tolist()
print("NumPy Array:", array)
print("Converted List:", list_result)
What’s happening here?
- We create a NumPy array using
np.array()
. - The
tolist()
method is then called on the array, and voilà! You now have a Python list.
Output:
NumPy Array: [1 2 3 4 5]
Converted List: [1, 2, 3, 4, 5]
Why choose tolist()
?
It’s reliable, simple, and works seamlessly even for more complex structures like multidimensional arrays (we’ll cover that soon).
b. Using the list()
Constructor (Alternate Method)
Now, here’s another way to tackle the problem. Python’s list()
constructor can also convert a NumPy array into a list. This method is straightforward but comes with a small caveat— it doesn’t handle nested arrays as well as tolist()
.
Let’s look at an example:
# Convert using list() constructor
list_result = list(array)
print("Converted List:", list_result)
Output:
Converted List: [1, 2, 3, 4, 5]
The Difference:
While the list()
constructor is quick and intuitive, it’s better suited for one-dimensional arrays. For multidimensional arrays, you’ll notice that it doesn’t automatically convert the nested structure into lists within lists. In such cases, tolist()
remains the go-to solution.
c. Handling Multidimensional Arrays
This might surprise you: working with multidimensional arrays is just as easy with tolist()
. It converts each dimension into nested lists, maintaining the array’s structure.
Here’s an example:
# Multidimensional NumPy array
array_2d = np.array([[1, 2, 3], [4, 5, 6]])
# Convert to list
list_result = array_2d.tolist()
print("NumPy Array:")
print(array_2d)
print("Converted List:")
print(list_result)
Output:
NumPy Array:
[[1 2 3]
[4 5 6]]
Converted List:
[[1, 2, 3], [4, 5, 6]]
What’s happening here?
- Each row in the array becomes a list within the final result.
- The hierarchical structure of the array is preserved, making
tolist()
ideal for handling multidimensional data.
Pro Tip:
If you’re working with large arrays, consider the size and complexity of your data. While both methods are efficient, the tolist()
method is better optimized for handling complex scenarios. Keep this in your toolbox for any compatibility challenges that might arise!
Conclusion
We’ve reached the final section of this guide, and by now, you should feel confident about converting NumPy arrays into Python lists.
It’s a straightforward process, right? Whether you use the tolist()
method or the list()
constructor, you’ve got all the tools to handle both simple and complex conversions.
Let me recap it for you:
- You learned the simplest way to convert NumPy arrays to lists using the
tolist()
method, which works flawlessly with multidimensional arrays. - You explored an alternative approach with the
list()
constructor, great for one-dimensional arrays but limited for nested structures. - You saw examples for both methods, with clear explanations and practical output demonstrations.
Now it’s your turn to put this into practice. I always say, “The best way to learn is by doing.” So go ahead — create your own NumPy arrays, try converting them, and even experiment with some edge cases. The more you experiment, the more confident you’ll become.
Closing thought:
Now that you know how to easily switch between these two data structures, you can handle compatibility challenges and work seamlessly in Python. Keep practicing, and you’ll master this in no time!
FAQs
Let’s tackle a couple of common questions you might have before wrapping up:
Q: What happens if I convert a large array?
The list will have the same elements as the original NumPy array, but you’ll lose NumPy-specific functionalities like vectorized operations and broadcasting.
Q: Can I convert a NumPy array with mixed data types?
Yes, absolutely! The resulting list will maintain the same mixed data types, but keep in mind that lists don’t have the optimized performance of NumPy arrays for numerical computations.