2

I have a list like this. Please pardon me if the title of the question is incorrect.

[array(['5029339601'], dtype=object), array(['5029339601'], dtype=object)]

I would like to convert this one to a normal list like

[ '5029339601' '5029339601']

I am sorry in advance for such a vague question. I would be grateful for your suggestions. Thanks

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Sam
  • 352
  • 2
  • 4
  • 22
  • Does this answer your question? [Converting NumPy array into Python List structure?](https://stackoverflow.com/questions/1966207/converting-numpy-array-into-python-list-structure) – sahasrara62 May 20 '21 at 15:56
  • I assume you're using NumPy, so I added the [tag:numpy] tag for you. – wjandrea Jul 31 '21 at 20:33

2 Answers2

0

Numpy arrays have .tolist() method for that

import numpy as np

arr1 = np.array('5029339601', dtype=object)
arr2 = np.array('5029339602', dtype=object)
lst = [arr1, arr2]
print(lst)

newlist = [item.tolist() for item in lst]
print(newlist)
S.B
  • 13,077
  • 10
  • 22
  • 49
0

numpy array's tolist() method will get you nested lists. There are a couple options for flattening lists. If the depth is consistent, you can flatten it with something like this. If the depth is not consistent, or not know in advance, you'll need to do some sort of recursive flattening.

Sarah Messer
  • 3,592
  • 1
  • 26
  • 43