# Python: How to create an empty numpy array?

## How to create an empty numpy array in Python?

To create an empty numpy array in Python, use the numpy array() or numpy empty() methods. Both options are explained with examples below. It is noteworthy that a numpy array is a very different data structure than a list in Python. It is to be used differently and behaves differently as well.## empty numpy array using array()

To create an empty numpy array, use the array() method of numpy as shown below.`# create an empty numpy array using array() method import numpy as np np.array([])`

In this example, an empty numpy array is created using the array() method of the numpy module. First, the numpy module is imported as np. Then, an empty list is passed to the np.array() method using empty square brackets. The empty list is then converted into a numpy array, which is empty because the passed list was empty as well. Finally, the empty numpy array is returned and the result is shown below.array([], dtype=float64)## empty numpy array using empty()

Another way is to use the numpy empty() function. The empty() function is useful to create numpy arrays of a given shape and type, without initializing entries.`# create an empty numpy array using empty() method import numpy as np np.empty((2,3), dtype=object)`

In this example, an empty numpy array of the dimensions 2 x 3 is created using the np.empty() method. First, the numpy module is imported as np. Then, the empty() method of the numpy module is used. The tuple (2,3) is passed to indicate the shape of the numpy array and the dtype parameter is specified as an object. Finally, the created numpy array is returned and the result is shown below.array([[None, None, None], [None, None, None]], dtype=object)## fill numpy array using append()

To fill the numpy array use the numpy append() function.`# fill numpy array using append() method import numpy as np myArray = np.array([]) myArray = np.append(myArray, 'new element') print(myArray)`

First, numpy is imported as np. Then an empty numpy array is created by passing an empty list to the numpy array() method. Then the append() method is used to append the string 'new element' as a new element to the numpy array. Finally, the numpy array is printed and the result is shown below.['new element']## convert list to a numpy array

If the data is already stored in some form, for example, a list in Python it is quite simple to convert it into a numpy array. An example is shown below.`# convert python list into numpy array import numpy as np myList = ['Python', 'PHP', 'C++', 'JavaScript'] myArray = np.array(myList) print(myArray)`

In this example, a common python list is converted into a numpy array. First, numpy is imported as np. Then, a list is defined, which contains the following elements: Python, PHP, C++, and JavaScript. Then, a numpy array is created using the array() method of numpy. Finally, the numpy array is printed. The result is shown below.['Python' 'PHP' 'C++' 'JavaScript']To confirm, that the created object is indeed a numpy array use the type() function. It returns the following class, indicating that a numpy array was successfully created.numpy.ndarray