Skip to content

[Python] How To Convert Numpy Data Format To Tuple Or List

When we talking about data processing related to arrays and multiple dimensions, you will think of Numpy. This is because Numpy as an optimized library composed of C/C++ and Fortran, and its computational efficiency is very fast.

But even so, when we use different tools, sometimes we still have to convert the Numpy data format into Tuple and List in Python.


Convert Numpy into Tuple

If there is only one dimension:

import numpy as np

np_arr = np.array([1, 2, 3])
tuple_arr = tuple(np_arr)



If it is a two-dimensional situation:

import numpy as np

np_arr = np.array([[1, 2], [3, 4]])
tuple_arr = tuple(map(tuple, np_arr))




Convert Numpy into List

It is easier to convert Numpy data into List. In Numpy, there is a method of constructing conversion:

import numpy as np

np_arr = np.array([[1, 2], [3, 4]])
list_arr = np_arr.tolist()




References


Read More

Leave a Reply