How To Turn A Numpy Array Into A List?

Before we start: This Python tutorial is a part of our series of Python Package tutorials. You can find other Numpy related topics too!

It is easy to transform a numpy array into a Python list using the numpy tolist() function or the list() function. Let’s take a look at the list() function first:

a = np.arange(4)
print(type(list(b)[0]))
print(type(list(b)))

Which will print the following.

<class 'numpy.int64'>
<class 'list'>

As you can see, the numpy array was transformed into a Python list, but the elements are still numpy built-in integer values. This is because using the list() function doesn’t recursively transform all elements. As a result, you can only use the list() function on one dimensional numpy arrays. 

Now let’s look at the tolist() function. Using our example above, we can apply the tolist() function as follows:

a = np.arange(4)
print(type(b.tolist()[0]))
print(type(b.tolist()))

The tolist() function can be used for any dimension numpy array of any shape:

a = np.arange(4).reshape(2,2)
a.tolist()

The above code snippet will create a 1D numpy array with digits from 0 to 3, reshape it to be a 2 by 2 array, and then print it out as a Python list.

Compiling from source can get quite complex, what with environment setup, scripts and patches, not to mention resolving any dependency conflicts or errors that may occur. Instead, consider using the ActivateState Platform to automatically build and package it for you.

The following tutorials will provide you with step-by-step instructions on how to work with Numpy, including:

Get a version of Python that’s pre-compiled for Data Science

While the open source distribution of Python may be satisfactory for an individual, it doesn’t always meet the support, security, or platform requirements of large organizations.

This is why organizations choose ActivePython for their data science, big data processing and statistical analysis needs.

Pre-bundled with the most important packages Data Scientists need, ActivePython is pre-compiled so you and your team don’t have to waste time configuring the open source distribution. You can focus on what’s important–spending more time building algorithms and predictive models against your big data sources, and less time on system configuration.

Some Popular Python Packages for Data Science/Big Data/Machine Learning You Get Pre-compiled – with ActivePython

  • pandas (data analysis)
  • NumPy (multi-dimensional arrays)
  • SciPy (algorithms to use with numpy)
  • HDF5 (store & manipulate data)
  • Matplotlib (data visualization)
  • Jupyter (research collaboration)
  • PyTables (managing HDF5 datasets)
  • HDFS (C/C++ wrapper for Hadoop)
  • pymongo (MongoDB driver)
  • SQLAlchemy (Python SQL Toolkit)

python for datascience

Use ActivePython and accelerate your Python projects.

  • The #1 Python solution used by innovative enterprise teams
  • Comes pre-bundled with top Python packages
  • Spend less time resolving dependencies and more time on quality coding

Take a look at ActivePython

Remi M