Python Create Numpy Array

import numpy as np

Let us create a numpy array from some random numbers. Check out more on how to generate random numbers in Python.

arr = np.array([10, 21, 3])
print(arr)
[10 21  3] 
type(arr)
numpy.ndarray

Convert Numpy (np) Array To list

list1 = arr.tolist()
print(f'List: {list1}')
List: [10, 21, 3] 
type(list1)
list

Converting Multi-Dimensional Array To List

import numpy as np
# 2d array to list
arr = np.array([[11, 100, 7], [14, 6, 2]]) 
list1 = arr.tolist() 
print(f'NumPy Array:\n{arr}') 
print(f'List: {list1}')
NumPy Array: [[ 11 100   7] [ 14   6   2]] 
List: [[11, 100, 7], [14, 6, 2]]

Python List To Numpy Array

l = [4,8,9]
arr = np.array(l)
print(arr)
array([4, 8, 9])