-3

How do I convert a NumPy array to a Python list, for example:

[[ 0.] [ 1.] [ 2.] [ 3.] [ 4.]] --> ['0', '1', '2', '3', '4']
3
  • my_array.tolist()??? Commented Apr 30, 2021 at 1:34
  • 2
    The array is (5,1) shape of floats. The list is length 5 of integer strings. Which of those differences is important? Why? Commented Apr 30, 2021 at 2:11
  • Does this answer your question? Converting NumPy array into Python List structure? Commented May 1, 2021 at 1:24

5 Answers 5

1

You can use numpy's .flatten() method followed by a list and map function.

>>> arr = np.array([[0.],[1.],[2.],[3.],[4.]])
>>> arr = arr.flatten()
>>> arr
array([0., 1., 2., 3., 4.])
>>> arr = list(map(lambda x:f"{x:.0f}", arr))
>>> arr
['0', '1', '2', '3', '4']
Sign up to request clarification or add additional context in comments.

1 Comment

Always use ravel over flatten when you need a read-only view
1

First try to understand what the array is:

In [73]: arr = np.array([[ 0.], [ 1.], [ 2.], [ 3.], [ 4.]])
In [74]: arr
Out[74]: 
array([[0.],
       [1.],
       [2.],
       [3.],
       [4.]])
In [75]: arr.shape, arr.dtype
Out[75]: ((5, 1), dtype('float64'))

There is a good, fast method to convert an array to list - that is close to the original in use of [] and type - floats.

In [76]: arr.tolist()
Out[76]: [[0.0], [1.0], [2.0], [3.0], [4.0]]

If you don't want those inner [], dimension, lets ravel/flatten array. That easier done with the array than the nested list:

In [77]: arr.ravel()
Out[77]: array([0., 1., 2., 3., 4.])

We could also convert the floats to strings:

In [78]: arr.ravel().astype(str)
Out[78]: array(['0.0', '1.0', '2.0', '3.0', '4.0'], dtype='<U32')
In [79]: arr.ravel().astype(str).tolist()
Out[79]: ['0.0', '1.0', '2.0', '3.0', '4.0']

But you want integers - so lets do that conversion first:

In [80]: arr.ravel().astype(int).astype(str).tolist()
Out[80]: ['0', '1', '2', '3', '4']

The other answer show how to do these conversion with lists.

Comments

0

you don't really need more than the following and the key part is numpy.reshape to remove your nested arrays. the rest is just casting to get rid of decimals and convert to strings, respectively:

# [[ 0.] [ 1.] [ 2.] [ 3.] [ 4.]] --> ['0', '1', '2', '3', '4']

import numpy as np

inputArray = np.array([[ 0.],[ 1.],[ 2.],[ 3.],[ 4.]])
inputArray = inputArray.reshape(-1).astype(np.int32).astype(str).tolist()

Comments

0

You can use a simple for loop:

import numpy as np

a = [[0.],[1.],[2.],[3.],[4.]]
b = np.asarray(a)
lst = []
for i in b:
    lst.append(str(int(i[0])))

# lst: ['0', '1', '2', '3', '4']

Comments

-1

Here's the code:

import numpy as np

arr = np.array([[1], [2], [3]])
print(f'NumPy Array: {arr}')

list1 = arr.tolist()
l = [i for sublist in list1 for i in sublist]

print(f'List: {l}')

2 Comments

My array like this [[ 0.] [ 1.] [ 2.] [ 3.] [ 4.]] not [1, 2, 3]
Modified the code to work as you requested. arr.tolist() converts numpy array to a 2D list. The list comprehension flattens 2D list to a single dimensional list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.