In this tutorial, I’ll show you how to convert from Numpy array to list. Essentially, we’ll take a Numpy array and convert it to a Python list using the tolist()
method.
In the tutorial, I’ll give you a quick introduction, I’ll explain the syntax, and I’ll show you a couple of simple examples of this technique.
If you need something specific, you can click on any of the following links, and the link will take you directly to the appropriate location in the post.
Table of Contents:
Ok. Let’s get into it.
A quick introduction Numpy tolist
Let’s start with a quick introduction to the technique.
If you’re reading this post, you’re probably already a little familiar with Numpy arrays and Python lists, but let’s quickly recap.
Lists and Numpy arrays are special data structures that store data in Python programs.
In particular, Numpy arrays are a special structure for storing numeric data and working with Numeric data. They store numeric data in a row-and-column structure that looks something like this:
We typically use Numpy arrays quite a bit for data science, machine learning, and scientific computing. When working with numeric data in Numpy, we commonly keep the data in array form, but there are some instances where we need to convert the array to a Python list.
The tolist
method converts from Numpy array to Python list
To accomplish this, we can use the tolist()
method from Numpy.
The tolist()
method takes a Numpy array and outputs a Python list.
It’s really very simple.
Having said that, let’s look at the syntax.
The syntax of the tolist method
The syntax of the Numpy tolist()
method is extremely simple. It’s arguably one of the simplest Numpy techniques.
You start by typing the name of a Numpy array.
This is important. Since the tolist()
technique is a method, you call it by typing the name of an object first.
After you type the name of your Numpy array, you use so-called “dot syntax” to call the method.
So you type a “dot” and then the name of the method, tolist()
.
That’s really it!
Obviously, this assumes that you already have a Numpy array of some kind.
For more information about how to generate different kinds of Numpy arrays, you might check out our tutorials about:
The Output of tolist
Once you call the tolist()
method, the output will be a Python list.
If the input array is a 1-dimensional Numpy array, then the output will be a simple 1D list.
If the input array is 2-dimensional or multi-dimensional, then the output will be a nested list.
Additionally, the elements of the input will be converted to the closest built-in Python data types.
Examples: How to Convert from a Numpy array to a Python List
Ok. Now that you’ve seen how the syntax works, let’s look at a couple of simple examples.
Examples:
Let’s start with example 1.
EXAMPLE 1: Convert a 1-dimensional array to a list
Here, we’re going to convert a simple 1-dimensional Numpy array to a Python list.
Create 1D Numpy Array
First, we need to create a 1-dimensional Numpy array.
To do this, we’ll use the Numpy arange function to create a 1D array that contains a sequence of values.
my_1d_array = np.arange(start = 1, stop = 6)
And let’s print it out to see the contents:
print(my_1d_array)
OUT:
[1 2 3 4 5]
Having said that, if we check the data type with type(my_1d_array)
, you’ll notice that my_1d_array is a numpy.ndarray
.
Convert Array to List
Now, let’s convert it to a list.
Here, we’ll use the tolist()
method and save the output to the name my_1d_list
.
my_1d_list = my_1d_array.tolist()
And lets print it out:
print(my_1d_list)
OUT:
[1, 2, 3, 4, 5]
Additionally, if we check the data type with the code type(my_1d_list)
, you’ll see that my_1d_list
is a list.
Explanation
This is really simple.
We typed the name of the Numpy array, and called the tolist()
method using “dot syntax.”
The output, my_1d_list
, essentially contains the same elements, but it’s a Python list instead of a Numpy array.
EXAMPLE 2: Convert a 2-dimensional array to a nested list
Next, we’l convert a 2-dimensional Numpy array to a nested Python list.
Create 2D Numpy Array
First, we need to create the 2-dimensional Numpy array.
To do this, we’ll use Numpy arange to create a sequence of values, and we’ll use the Numpy reshape method to re-shape that 1D array into a 2D array.
my_2d_array = np.arange(start = 1, stop = 7).reshape(2,3)
And let’s print it out to see the contents:
print(my_2d_array)
OUT:
[[1 2 3] [4 5 6]]
And again, if we check the data type with type(my_2d_array)
, you’ll notice that my_2d_array is a numpy.ndarray
.
Convert Array to List
Now, let’s convert the array to a list.
Once again, we’ll use the tolist()
method. And we’ll save the output to the name my_2d_list
.
my_1d_list = my_1d_array.tolist()
And lets print it out:
print(my_2d_list)
OUT:
[[1, 2, 3], [4, 5, 6]]
If you check the data type with the code type(my_2d_list)
, you’ll see that my_2d_list
is a Python list.
Explanation
Again, this is really simple.
To convert from a Numpy array to list, we simply typed the name of the 2D Numpy array, and then called the Numpy tolist()
method which produced a Python list as an output.
Moreover, take a look at the output list itself: [[1, 2, 3], [4, 5, 6]]
From the structure, we can see that this is a nested Python list. Two lists of 3 elements each, that exist within a larger list. It’s a list-of-lists.
This is how the Numpy tolist
method handles multi-dimensional input arrays. When tolist
operates on a multi-dimensional input, it produces a nested array as an output.
Leave your other questions in the comments below
Do you have any other questions about the Numpy tolist
method?
If so, leave your questions in the comments section near the bottom of the page.
Join our course to learn more about Numpy
Are you interested in learning more about Numpy?
This tutorial should have shown you how to use the Numpy tolist()
method, but to master numeric data manipulation in Python, there’s a lot more to learn.
If you’re serious about learning Numpy, you should join our premium course, Numpy Mastery.
In this course, you’ll learn everything you need to know about Numpy.
- How to create Numpy arrays
- How to use the Numpy random functions
- What Numpy axes are, and how to use them
- What the “Numpy random seed” function does
- How to reshape, split, and combine your Numpy arrays
- and much more …
Additionally, when you join, you’ll get access to our unique practice system.
This practice system will enable you to memorize all of the syntax that you learn. If you take this course and practice like we show you, you’ll be able to write Numpy code fluently, accurately, and 100% from memory.
Find out more here:
Informative
👍👍👍