Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Converting a NumPy Array to Python List

  • 17 Jan, 2025
  • Com 0
Converting Numpy Array to Python List

When you want to work with data of different types, a Numpy array is not feasible. That’s where a list comes into the picture.

When you convert the Numpy array to a list, each element is created as a new Python object. This means the data is copied from the contiguous NumPy array memory to the scattered Python list memory.

During the conversion, NumPy data types (int64, float64) are converted to their corresponding Python types (e.g., int, float). If it fails to convert by default, we need to convert manually.

Here are three ways to convert a Numpy array to a Python List:

  1. Using tolist()
  2. Using list()
  3. Using list comprehension

Method 1: Using tolist()

The most efficient and recommended method is the tolist() method, which recursively converts the array to a nested list structure. The tolist() method does not accept any argument and easily converts one-dimensional, two-dimensional, or multi-dimensional array to a simple list or nested list.

One-dimensional array

Converting a NumPy Array to Python List using .tolist() method

import numpy as np

numpy_arr_1d = np.array([11, 18, 19, 21, 46])
print(numpy_arr_1d)  # [11 18 19 21 46]
print(type(numpy_arr_1d))  # <class 'numpy.ndarray'>

simple_list = numpy_arr_1d.tolist()

print(simple_list)  # [11, 18, 19, 21, 46]
print(type(simple_list))  # <class 'list'>

Here, we defined a one-dimensional numpy array and operated on the tolist() function. The type() function is used to get the type of object, which is changed from numpy.ndarray to list.

Two-dimensional array

Converting Two-dimensional array into a list in Python

import numpy as np

numpy_arr_2d = np.array([[11, 21], [19, 18]])
print(numpy_arr_2d)  # [[11 21]
                     #  [19 18]]
print(type(numpy_arr_2d))  # <class 'numpy.ndarray'>

nested_list = numpy_arr_2d.tolist()

print(nested_list)  # [[11, 21], [19, 18]]
print(type(nested_list))  # <class 'list'>

We defined a 2D numpy array and used the same tolist() function to convert it into a nested list.

Three-dimensional array

import numpy as np

numpy_arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(numpy_arr_3d)  # [[[1 2]
                     #   [3 4]]
                     #  [[5 6]
                     #   [7 8]]]
print(type(numpy_arr_3d))  # <class 'numpy.ndarray'>

deep_nested_list = numpy_arr_3d.tolist()

print(deep_nested_list)  # [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
print(type(deep_nested_list))  # <class 'list'>

You can see that the tolist() method can handle a multi-dimensional array and gives proper output.

Pros

  1. It can quickly deal with multi-dimensional arrays where you must preserve the structure.
  2. It is efficient, simple, and concise.

Cons

  1. It can become less efficient for large arrays.

Method 2: Using list()

Method 2 - Using list() function to convert an array into a list

The list() constructor directly operates on a one-dimensional numpy array and returns a list. However, this conversion has one caveat.

Since numpy arrays use their own data types like np.int64, when we convert the array to a list, the type of the elements (np.int64) is retained.

If you want the elements in the Python list to be standard Python integers (int), we can explicitly convert them during the conversion process using map() and int() functions.

import numpy as np

numpy_arr_1d = np.array([11, 18, 19, 21, 46])
print(numpy_arr_1d)  # [11 18 19 21 46]
print(type(numpy_arr_1d))  # <class 'numpy.ndarray'>

main_list = list(map(int, numpy_arr_1d))

print(main_list)  # [11, 18, 19, 21, 46]
print(type(main_list))  # <class 'list'>

Pros

  1. It is efficient for one-dimensional (1D) arrays.

Cons

  1. It can only operate on 1D arrays and not directly work with multidimensional arrays. If you want to work with them anyway, iterate through each sub-array and use the list() function on each.

Method 3: List Comprehension

Method 3 - List Comprehension

When flattening an array, list comprehension gives more conversion process control. Our final output will be a flattened list, not a nested list.

import numpy as np

numpy_arr_2d = np.array([[11, 21], [19, 18]])
print(numpy_arr_2d)  # [[11 21]
                     #  [19 18]]
print(type(numpy_arr_2d))  # <class 'numpy.ndarray'>

main_list = [int(item) for sublist in numpy_arr_2d for item in sublist] # [11, 21, 19, 18]

print(main_list)  # [11, 21, 19, 18]
print(type(main_list))  # <class 'list'>

Before converting, we flatten the array into a 1D array and then use the int() function and list comprehension.

The final output is a simple list with the exact elements as an input array.

Pros

  1. It is efficient when you want to flatten the input object.
  2. You can modify the object’s structure however you want, which gives you immense power.

Cons

  1. It is not a reader-friendly code.
  2. It is not as efficient as the tolist() function when it comes to multidimensional arrays.

That’s all!

Post Views: 26
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Reverse Numpy Array (1D and Multi-Dimensional)
Converting List to JSON in Python (Variables and Files)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend