List of arrays to list of lists Python

In this tutorial, Ill show you how to convert from Numpy array to list. Essentially, well take a Numpy array and convert it to a Python list using the tolist[] method.

In the tutorial, Ill give you a quick introduction, Ill explain the syntax, and Ill 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:

  • Introduction
  • Syntax
  • Examples
  • Frequently Asked Questions

Ok. Lets get into it.

A quick introduction Numpy tolist

Lets start with a quick introduction to the technique.

If youre reading this post, youre probably already a little familiar with Numpy arrays and Python lists, but lets 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.

Its really very simple.

Having said that, lets look at the syntax.

The syntax of the tolist method

The syntax of the Numpy tolist[] method is extremely simple. Its 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[].

Thats 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:

  • Numpy random normal
  • the Numpy random randint function
  • the Numpy arange function

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 youve seen how the syntax works, lets look at a couple of simple examples.

Examples:

  • Convert a 1-dimensional array to a list
  • Convert a 2-dimensional array to a nested list

Lets start with example 1.

EXAMPLE 1: Convert a 1-dimensional array to a list

Here, were 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, well 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 lets 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], youll notice that my_1d_array is a numpy.ndarray.

Convert Array to List

Now, lets convert it to a list.

Here, well 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], youll 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 its a Python list instead of a Numpy array.

EXAMPLE 2: Convert a 2-dimensional array to a nested list

Next, wel 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, well use Numpy arange to create a sequence of values, and well 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 lets 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], youll notice that my_2d_array is a numpy.ndarray.

Convert Array to List

Now, lets convert the array to a list.

Once again, well use the tolist[] method. And well 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], youll 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. Its 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, theres a lot more to learn.

If youre serious about learning Numpy, you should join our premium course, Numpy Mastery.

In this course, youll 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, youll 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, youll be able to write Numpy code fluently, accurately, and 100% from memory.

Find out more here:

Learn More About Numpy Mastery

Video liên quan

Chủ Đề