How do I turn an array into a list?

NumPy Array to List

The tolist[] function doesn’t accept any argument. It’s a simple way to convert an array to a list representation.

1. Converting one-dimensional NumPy Array to List

import numpy as np # 1d array to list arr = np.array[[1, 2, 3]] print[f'NumPy Array:\n{arr}'] list1 = arr.tolist[] print[f'List: {list1}']

Output:

NumPy Array: [1 2 3] List: [1, 2, 3]

2. Converting multi-dimensional NumPy Array to List

import numpy as np # 2d array to list arr = np.array[[[1, 2, 3], [4, 5, 6]]] print[f'NumPy Array:\n{arr}'] list1 = arr.tolist[] print[f'List: {list1}']

Output:

NumPy Array: [[1 2 3] [4 5 6]] List: [[1, 2, 3], [4, 5, 6]]

Reference: API Doc

Python | Convert an array to an ordinary list with the same items

Prerequisite : Array in Python

Python program to convert an array to an ordinary list with the same items.

Examples:

Input : array['i', [1, 3, 5, 3, 7, 1, 9, 3]] Output :[1, 3, 5, 3, 7, 1, 9, 3] Explanation: the array with elements [1, 3, 5, 3, 7, 1, 9, 3] are converted into list with the same elements. Input :array['k', [45, 23, 56, 12]] Output :[45, 23, 56, 12] Explanation: the array with elements [45, 23, 56, 12] are converted into list with the same elements.

Approach to the problem:
We want to convert an array into an ordinary list with the same items. For doing so we need to use a function

// This function tolist[] converts the array into a list. arrayname.tolist[]

1. Overview

In this quick tutorial, we're going to learn how to convert between an Array and a List using core Java libraries, Guava and Apache Commons Collections.

This article is part of the “Java – Back to Basic” series here on Baeldung.

Java Array to List

In Java, Array and List are the two most important data structures. In this section, we will learn how to convert Java Array into a List. We have also created Java programs that convert Array into a List by using different Java methods.

numpy.ndarray.tolist¶

method

ndarray.tolist[]

Return the array as an a.ndim-levels deep nested list of Python scalars.

Return a copy of the array data as a [nested] Python list. Data items are converted to the nearest compatible builtin Python type, via the item function.

If a.ndim is 0, then since the depth of the nested list is 0, it will not be a list at all, but a simple Python scalar.

ParametersnoneReturnsyobject, or list of object, or list of list of object, or …

The possibly nested list of array elements.

Notes

The array may be recreated via a = np.array[a.tolist[]], although this may sometimes lose precision.

Examples

For a 1D array, a.tolist[] is almost the same as list[a], except that tolist changes numpy scalars to Python scalars:

>>> a = np.uint32[[1, 2]] >>> a_list = list[a] >>> a_list [1, 2] >>> type[a_list[0]] >>> a_tolist = a.tolist[] >>> a_tolist [1, 2] >>> type[a_tolist[0]]

Additionally, for a 2D array, tolist applies recursively:

>>> a = np.array[[[1, 2], [3, 4]]] >>> list[a] [array[[1, 2]], array[[3, 4]]] >>> a.tolist[] [[1, 2], [3, 4]]

The base case for this recursion is a 0D array:

>>> a = np.array[1] >>> list[a] Traceback [most recent call last]: ... TypeError: iteration over a 0-d array >>> a.tolist[] 1

Python: Convert an array to an ordinary list with the same items

Last update on January 02 2021 11:01:14 [UTC/GMT +8 hours]

Video liên quan

Bài mới nhất

Chủ Đề